2. DECIMAL TO BINARY CONVERSION
600 / 2 = 300 r 0 [least significant bit - lsb]
300 / 2 = 150 r 0
150 / 2 = 75 r 0
75 / 2 = 37 r 1
37 / 2 = 18 r 1
18 / 2 = 9 r 0
9 / 2 = 4 r 1
4 / 2 = 2 r 0
2 / 2 = 2 r 0
1 / 2 = 0 r 1 [most significant bit - msb]
3. Decimal to Hexadecimal Conversion
5789 ÷ 16 = 361 r 13 or D LSB
361 ÷ 16 = 22 r 9 or 9
22 ÷ 16 = 1 r 6 or 6
1 ÷ 16 = 0 r 1 or 1 MSB
5789 in decimal = 169D in hexadecimal.
4. Negative numbers
On paper, a negative number is represented by a dash – in front of
the number.
A computer can only ADD positive numbers
A computer can only work with 0's and 1's.
The solution??
Method of complements
http://en.wikipedia.org/wiki/Method_of_complements
5. Before digital computers, mechanical calculators used the
9's complement numbering method, as it could only
subtract by adding together positive numbers.
See :
http://en.wikipedia.org/wiki/Method_of_complements#Decimal_example
for more information.
6. The binary 'sign' bit
An 8-bit binary number (data type 'int' in C, 'byte' in Java) can be
used to represent negative numbers by 'hijacking' the MSB for use
as a 'sign bit'.
With 8 bits you can represent numbers which range from:
(Sign bit)+ ----->0] 0 0 0 0 0 0 0 (+0 in decimal)
(Sign bit)- ----->1] 0 0 0 0 0 0 0 (-0 in decimal)
(Sign bit)+ ----->0] 1 1 1 1 1 1 1 (+127 in decimal)
(Sign bit)- ----->1] 1 1 1 1 1 1 1 (-127 in decimal)
The first number represents whether the number is positive or
negative.
0 = + 1 = -
7. For example::
00000111 represents 7 in decimal
10000111 represents -7 in decimal
So:
00000000 to 01111111 represents 0 to 127
and
10000000 to 11111111 represents 0 to -127
Notice that there are two representations for zero
00000000 and 10000000
This is a problem.
8. Complementary numbers
Complementary numbers are found by counting backwards from the
highest number available. For an 8-bit number this is:
11111111
Counting backwards we get::
11111111 = -0
11111110 = -1
11111101 = -2
11111100 = -3
11111011 = -4
This is known as the 1's complement of a positive number.
9. So a complementary number is an inverted 'normal' number, where
a negative number is made by 'bitwise inverting' the positive
number.
00000000 = +0
11111111 = -0 (silly)
00000001 = +1
11111110 = -1
00000010 = +2
11111101 = -2
Notice that 00000000 and 11111111 both represent zero, which
is a 'waste' of a binary number.
10. To make better use of the complementary number range, it would
make better sense to let 00000000 represent zero, and 11111111
to represent -1 instead of -0, which is silly.
So take the 1's complement and add 1 to it. Now it is 11111111 that
represents -1 rather than 11111110. This is shown here:
11111110 + 00000001 = 11111111 = (or represents) -1
11111101 + 00000001 = 11111110 = (or represents) -2
11111100 + 00000001 = 11111101 = (or represents) -3
11111011 + 00000001 = 11111100 = (or represents) -4
11111010 + 00000001 = 11111011 = (or represents) -5
11111001 + 00000001 = 11111010 = (or represents) -6
11111000 + 00000001 = 11111001 = (or represents) -7
1's complement + 1 = 2's complement
So 1's complement + 1 is known as the 'true' or 2's complement.
11. So.
To find a 2's complement representation of (say) the number -7
start with +7, ie.
00000111
Then invert (NOT) every bit to get the 1's complement.
11111000
Then add 1 to the number to get the 2's complement.
11111000 + 1 = 11111001
The MSB is a 1 so the number is negative, and this number now
REPRESENTS -7 for calculation purposes.
12. Subtraction in Binary
Computers can only move binary information, and add it together.
They cannot directly perform multiplication, subtraction or division.
Subtraction of two binary numbers can only be done in a computer
by adding together the normal representation of a positive number
to the 2's complement of the negative number.
For example::
45 - 20 = 25
This can be expressed as:
+45 + (-20) = +25
13. +45 = 0010 1101 normal representation
+20 = 0001 0100 normal representation
-20 = 1110 1011 1's complement
+ 0000 0001 add 1 to give:
-20 = 1110 1100 2's complement
Note the process to get the 2's complement
Now add:
+45 = 0010 1101 normal representation
-20 = 1110 1100 2's complement
= (1)0001 1001
= 25 in decimal
The "overflow" 9th bit above is ignored.