From time immemorial, we have used the decimal system for counting. The reason is that we have a total of ten digits on our hands which are valuable in addition. There are ten unique digits in the system, namely 0 1 2 3 4 5 6 7 8 and 9. All other numbers are constructed by combining two or more of these digits in any order. But a computer does not have fingers and thumbs. Instead it has switches. Switches have only two positions, Off and On. So we use a system based on two digits known as Base two or Binary. Off is represented by 0 and On by 1. The two binary digits 0 and 1 are also called bits. Since 8 and 16 are powers of 2, it is easy to convert a binary system to a Base 8 or Octal and a Base 16 or Hexadecimal system. The eight digits in the Octal system are 0 1 2 3 4 5 6 and 7, while the 16 digits in the Hexadecimal system are 0 1 2 3 4 5 6 7 8 9 A B C D E and F. The base is also called Radix.
The table below gives the decimal numbers with their binary, octal,
and hexadecimal equivalents.
|
|
|
|
|
|
0
|
0
|
0
|
0
|
|
1
|
1
|
1
|
1
|
|
2
|
10
|
2
|
2
|
|
3
|
11
|
3
|
3
|
|
4
|
100
|
4
|
4
|
|
5
|
101
|
5
|
5
|
|
6
|
110
|
6
|
6
|
|
7
|
111
|
7
|
7
|
|
8
|
1000
|
10
|
8
|
|
9
|
1001
|
11
|
9
|
|
10
|
1010
|
12
|
A
|
|
11
|
1011
|
13
|
B
|
|
12
|
1100
|
14
|
C
|
|
13
|
1101
|
15
|
D
|
|
14
|
1110
|
16
|
E
|
|
15
|
1111
|
17
|
F
|
|
16
|
10000
|
20
|
10
|
|
20
|
10100
|
24
|
14
|
|
30
|
11110
|
36
|
1E
|
|
40
|
101000
|
50
|
28
|
|
50
|
110010
|
62
|
32
|
|
60
|
111100
|
74
|
3C
|
|
70
|
1000110
|
106
|
46
|
|
80
|
1010000
|
120
|
50
|
|
90
|
1011010
|
132
|
5A
|
|
100
|
1100100
|
144
|
64
|
|
1000
|
1111101000
|
1750
|
3E8
|
|
2989
|
101110101101
|
5655
|
BAD
|
CONVERSION FROM ONE RADIX TO ANOTHER
To convert a decimal number into any other divide by the radix. Divide by the radix, that is by 2 to convert to binary, by 8 for octal and by 16 for hexadecimal, etc. Write the quotient under the original number, and the remainder at its side. Continue dividing until the quotient is zero, noting the remainder at each stage. Reverse the order of the remainders, that is the last remainder is the first digit, and the first remainder is the last digit. This yields the binary, the octal, or the hexadecimal, or any other required radix number. An example is given below for converting 1492 to its binary equivalent.
Quotients Remainders
1 4 9 2
7 4 6 0
3 7 3 0
1 8 6 1
9 3 0
46 1
23 0
11 1
5 1
2 1
1 0
0 1
Reversing the order of the remainders, the binary equivalent is 1 0
1 1 1 0 1 0 1 0 0
That is 149210 is equivalent to 101110101002
To convert from binary to decimal, sum up the powers of 2 corresponding to the 1 bit. The rightmost bit is 2 0 the one to its left is 21 and so on. Ignore the 0 bit. For example
1 0 1 1 0
24 23
22 21 20 The binary
equivalent is 1 x 24 + 0 x 23 + 1 x 22
+ 1 x 21 + 0 x 20 This equals 16 + 4 + 2 =
22.
16 8 4
2 1
To convert number from any other radix, use the powers of that radix.
To convert a binary number to octal, divide it into groups of
three bits starting from both sides of the decimal point. Each group is
then converted into digits 0 to 7. To convert from binary to hexadecimal,
the process is the same except we use four rather than three bits in each
group. This is then converted to digits 0 to F. In reverse
conversion, each digit is replace by three (for octal) or four (for hexadecimal)
binary digits. In this case conversion of numbers after the decimal
point is also easy. See the two examples in the textbook on page
636.
Four different systems are used to represent negative numbers in binary.
1. Signed magnitude. The leftmost bit is the sign bit, o for positive and 1 for negative. The remaining bits constitute the absolute magnitude of the number.
2. One's complement. To obtain the negative of a number replace every 0 bit by 1 and every 1 bit by 0. This can also be achieved by subracting each number from one. This system is now obsolete.
3. Two's complement. This is a two step. The first step is the same as for one's complement, that is replacing every 0 by 1 and every 1 by 0 (or subtract each digit from 1). The second step is to add 1 to the result. Any carry occurring from the leftmost bit is discarded.
4. Excess 2m - 1. A number is negated by adding 2m - 1, where m is the number of bits. If there are 8 bits in the number, m = 8 - 1 = 7. 27 = 128. So 128 is added to the negative of the number. -3 is stored as -3 + 128 = 125. In binary 125 is 1111101. Using 8 bits, -310 is 011111012.
Both signed magnitude and one's complement have two representations
for zero: a plus zero and a minus zero, which is undesirable. In
two's complement a negative of plus zero is also plus zero. Also
having 8 bits, there is no negative for 128 in binary, in signed magnitude
and in one's complement. The table below gives negative 8-bit numbers
in all four systems.
|
decimal |
binary |
signed magnitude |
one's complement |
two's complement |
excess 128 |
|
0
|
00000000
|
10000000
|
11111111
|
00000000
|
|
|
1
|
00000001
|
10000001
|
11111110
|
11111111
|
01111111
|
|
2
|
00000010
|
10000010
|
11111101
|
11111110
|
01111110
|
|
3
|
00000011
|
10000011
|
11111100
|
11111101
|
01111101
|
|
4
|
00000100
|
10000100
|
11111011
|
11111100
|
01111100
|
|
5
|
00000101
|
10000101
|
11111010
|
11111011
|
01111011
|
|
6
|
00000110
|
10000110
|
11111001
|
11111010
|
01111010
|
|
7
|
00000111
|
10000111
|
11111000
|
11111001
|
01111001
|
|
8
|
00001000
|
10001000
|
11110111
|
11111000
|
01111000
|
|
9
|
00001001
|
10001001
|
11110110
|
11110111
|
01110111
|
|
10
|
00001010
|
10001010
|
11110101
|
11110110
|
01110110
|
|
20
|
00010100
|
10010100
|
11101011
|
11101100
|
01101100
|
|
30
|
00011110
|
10011110
|
11100001
|
11100010
|
01100010
|
|
40
|
00101000
|
10101000
|
11010111
|
11011000
|
01011000
|
|
50
|
00110010
|
10110010
|
11001101
|
11001110
|
01001110
|
|
60
|
00111100
|
10111100
|
11000011
|
11000100
|
01000100
|
|
70
|
01000110
|
11000110
|
10111001
|
10111010
|
00111010
|
|
80
|
01010000
|
11010000
|
10101111
|
10110000
|
00110000
|
|
90
|
01011010
|
11011010
|
10100101
|
10100110
|
00100110
|
|
100
|
01100100
|
11100100
|
10011011
|
10011100
|
00011100
|
|
127
|
01111111
|
11111111
|
10000000
|
10000001
|
00000001
|
|
128
|
|
|
|
10000000
|
00000000
|
Note: 128 does not exist in signed 8-bit binary as 12810 is equivalent to 100000002. But since the first bit is a sign bit and since 1 is negative, this really is equivalent to -0. However in unsigned 8-bit binary, 12810 is equivalent to 100000002. Similarly 0 does not exist in excess 128 as it equals 10000000. But again the first bit is a sign bit. In excess 128, all numbers are positive. Also note that except for the sign bit, all magnitude bits are same in two's complement and excess 2m - 1.
1. Convert the following numbers to Binary: 1984, 4000, 8192
2. What is 1001101001 (Binary) in Decimal? In Octal? In Hexadecimal?
3. Which of the following are valid hexadecimal numbers? BED, CAB, DEAD, DECADE, ACCEDED, BAG, DAD.
4. Express the decimal number 100 in all radices from 2 to 9
5. How many different positive integers can be expressed in k digits using radix r numbers?
6. Most people can only count to 10 on their fingers; however, computer scientists can do better. If you regard each finger as one binary bit, with fingers extended as 1 and finger touching palm as 0, how high can you count using both hands? With both hands and both feet? Now use both hands and both feet, with the big toe on your left foot as a sign bit for two's complement numbers. What is the range of expressible numbers?
7. Perform the following calculations on 8-bit two's complement numbers
00101101
11111111 00000000
11110111
+ 01101111
+ 11111111 -
11111111 - 11110111
8. Repeat the calculations of the preceding problem but now in one's complement.
9. Consider the following addition problems for 3-bit
binary numbers . For each sum, state
a. Whether the sign bit of the
result is 0 or 1
b. Whether the low-order 3 bits
are 0 or 1
c. Whether an overflow occurred.
000
000 111
100 100
+001
+111 +110
+111 +100
10. Signed decimal numbers consisting of n digits can be represented in n + 1 digits without a sign. positive numbers have 0 as the leftmost digit. Negative numbers are formed by subtracting each digit from 9. Thus the negative of 014725 is 985274. Such numbers are called nine's complement numbers and are analogous to one's complement binary numbers. Express the following as three-digit nine's complement numbers: 6, -2, 100, -14, -1, 0.
11. Determine the rule for addition on nine's complement numbers and then perform the following additions.
0001
0001
9997 9241
+ 9999
+ 9998 + 9996
+ 0802
.
14. Multiply 0111 by 0011 in binary.
1. The numbers are: 11111000000, 111110100000, and 10000000000000
2. In decimal it is 617, in octal it is 1151, and in hexadecimal it is 269.
3. All strings containing the ten digits and the letters A, B, C, D, E, and F are valid hexadecimal numbers. All the examples given are valid except BAG.
4. The answers are: 1100100, 10201, 1210, 400, 244, 202, 144, and 121.
5. The number of valid strings of length k with r possibilities in each position is rk
6. With just your hands you have 10 bits, so you can count to 210 - 1 or 1023. With hand and feet the limit is 220-1 or 1,048,575. In two's complement the range is -219 to 219 - 1 or -524,288 to +524,287.
7. The results are: 10011100, 11111110, 00000001, 00000000. If the calculations results in an overflow, ignore the first digit. Use only 8 bits not 9.
8. The results are: 10011100, 11111110, 00000000, 11111111. The overflow is added. Use only 8 bits.
9. The five sums are (use only 3 bits):
001, 111, 101, 011, and 000.
a. The sign bits (first bits)
are: 0, 1, 1, 0, and 0.
b. The low order bits are:
1, 1, 1, 1, and 0.
c. Overflow?
No, No, Yes, Yes, and Yes.
10. The numbers are: 006, 997, 100, 985, 998, and 000.
11. Add the numbers. If there is an overflow, add 1. The answers are: 0001, 9999, 9994, and 0044.
14. The answer is 10101 (7 x 3 = 21).