A binary digit is called a bit. There are two possible states in a bit, usually expressed as 0 and 1.
A series of eight bits strung together makes a byte, much as 12 makes a dozen. With 8 bits, or 8 binary digits, there exist 2^8=256 possible combinations. The following table shows some of these combinations. (The number enclosed in parentheses represents the decimal equivalent.)
00000000 ( 0) 00010000 ( 16) 00100000 ( 32) ... 01110000 (112) 00000001 ( 1) 00010001 ( 17) 00100001 ( 33) ... 01110001 (113) 00000010 ( 2) 00010010 ( 18) 00100010 ( 34) ... 01110010 (114) 00000011 ( 3) 00010011 ( 19) 00100011 ( 35) ... 01110011 (115) 00000100 ( 4) 00010100 ( 20) 00100100 ( 36) ... 01110100 (116) 00000101 ( 5) 00010101 ( 21) 00100101 ( 37) ... 01110101 (117) 00000110 ( 6) 00010110 ( 22) 00100110 ( 38) ... 01110110 (118) 00000111 ( 7) 00010111 ( 23) 00100111 ( 39) ... 01110111 (119) 00001000 ( 8) 00011000 ( 24) 00101000 ( 40) ... 01111000 (120) 00001001 ( 9) 00011001 ( 25) 00101001 ( 41) ... 01111001 (121) 00001010 ( 10) 00011010 ( 26) 00101010 ( 42) ... 01111010 (122) 00001011 ( 11) 00011011 ( 27) 00101011 ( 43) ... 01111011 (123) 00001100 ( 12) 00011100 ( 28) 00101100 ( 44) ... 01111100 (124) 00001101 ( 13) 00011101 ( 29) 00101101 ( 45) ... 01111101 (125) 00001110 ( 14) 00011110 ( 30) 00101110 ( 46) ... 01111110 (126) 00001111 ( 15) 00011111 ( 31) 00101111 ( 47) ... 01111111 (127) : (continued) : 10000000 (128) 10010000 (144) 10100000 (160) ... 11110000 (240) 10000001 (129) 10010001 (145) 10100001 (161) ... 11110001 (241) 10000010 (130) 10010010 (146) 10100010 (162) ... 11110010 (242) 10000011 (131) 10010011 (147) 10100011 (163) ... 11110011 (243) 10000100 (132) 10010100 (148) 10100100 (164) ... 11110100 (244) 10000101 (133) 10010101 (149) 10100101 (165) ... 11110101 (245) 10000110 (134) 10010110 (150) 10100110 (166) ... 11110110 (246) 10000111 (135) 10010111 (151) 10100111 (167) ... 11110111 (247) 10001000 (136) 10011000 (152) 10101000 (168) ... 11111000 (248) 10001001 (137) 10011001 (153) 10101001 (169) ... 11111001 (249) 10001010 (138) 10011010 (154) 10101010 (170) ... 11111010 (250) 10001011 (139) 10011011 (155) 10101011 (171) ... 11111011 (251) 10001100 (140) 10011100 (156) 10101100 (172) ... 11111100 (252) 10001101 (141) 10011101 (157) 10101101 (173) ... 11111101 (253) 10001110 (142) 10011110 (158) 10101110 (174) ... 11111110 (254) 10001111 (143) 10011111 (159) 10101111 (175) ... 11111111 (255)
"0", "1", "2", "3", "4", "5", "6", "7".In the decimal system, there are ten different numbers that can enter the digit box:
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9".In the hexadecimal system, we allow 16 numbers:
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", and "F".As demonstrated by the following table, there is a direct correspondence between the binary system and the octal system, with three binary digits corresponding to one octal digit. Likewise, four binary digits translate directly into one hexadecimal digit. In computer usage, hexadecimal notation is especially common because it easily replaces the binary notation, which is too long and human mistakes in transcribing the binary numbers are too easily made.
Base Conversion Table
BIN OCT HEX DEC ---------------------- 0000 00 0 0 0001 01 1 1 0010 02 2 2 0011 03 3 3 0100 04 4 4 0101 05 5 5 0110 06 6 6 0111 07 7 7 ---------------------- 1000 10 8 8 1001 11 9 9 1010 12 A 10 1011 13 B 11 1100 14 C 12 1101 15 D 13 1110 16 E 14 1111 17 F 15
Original Number: 1 2 3 4 | | | | How Many Tokens: 1 2 3 4 Digit/Token Value: 1000 100 10 1 Value: 1000 + 200 + 30 + 4 = 1234 or simply, 1*1000 + 2*100 + 3*10 + 4*1 = 1234Thus, each digit has a value: 10^0=1 for the least significant digit, increasing to 10^1=10, 10^2=100, 10^3=1000, and so forth.
Likewise, the least significant digit in a hexadecimal number has a value of 16^0=1 for the least significant digit, increasing to 16^1=16 for the next digit, 16^2=256 for the next, 16^3=4096 for the next, and so forth. Thus, 1234 means that there are four boxes (digits); and there are 4 one's in the right-most box (least significant digit), 3 sixteen's in the next box, 2 256's in the next, and 1 4096's in the left-most box (most significant digit). The total is:
1*4096 + 2*256 + 3*16 + 4*1 = 4660
Example. Convert the hexadecimal number 4B3 to decimal notation. What about the decimal equivalent of the hexadecimal number 4B3.3?
Solution:
Original Number: 4 B 3 . 3
| | | |
How Many Tokens: 4 11 3 3
Digit/Token Value: 256 16 1 0.0625
Value: 1024 +176 + 3 + 0.1875 = 1203.1875
Example. Convert 234.14 expressed in an octal notation to decimal.
Solution:
Original Number: 2 3 4 . 1 4
| | | | |
How Many Tokens: 2 3 4 1 4
Digit/Token Value: 64 8 1 0.125 0.015625
Value: 128 + 24 + 4 + 0.125 + 0.0625 = 156.1875
Another way is to think of a cash register with different slots, each holding bills of a different denomination.
1234/10 = 123 + 4/10The remainder of 4 is the last digit. To extract the next last digit, you again move the decimal point left by one digit and see what drops out.
123/10 = 12 + 3/10The remainder of 3 is the next last digit. You repeat this process until there is nothing left. Then you stop. In summary, you do the following:
Quotient Remainder ----------------------------- 1234/10 = 123 4 --------+ 123/10 = 12 3 ------+ | 12/10 = 1 2 ----+ | | 1/10 = 0 1 --+ | | | (Stop when the quotient is 0.) | | | | 1 2 3 4 (Base 10)Now, let's try a nontrivial example. Let's express a decimal number 1341 in binary notation. Note that the desired base is 2, so we repeatedly divide the given decimal number by 2.
Quotient Remainder ----------------------------- 1341/2 = 670 1 ----------------------+ 670/2 = 335 0 --------------------+ | 335/2 = 167 1 ------------------+ | | 167/2 = 83 1 ----------------+ | | | 83/2 = 41 1 --------------+ | | | | 41/2 = 20 1 ------------+ | | | | | 20/2 = 10 0 ----------+ | | | | | | 10/2 = 5 0 --------+ | | | | | | | 5/2 = 2 1 ------+ | | | | | | | | 2/2 = 1 0 ----+ | | | | | | | | | 1/2 = 0 1 --+ | | | | | | | | | | (Stop when the quotient is 0) | | | | | | | | | | | 1 0 1 0 0 1 1 1 1 0 1 (BIN; Base 2)Let's express the same decimal number 1341 in octal notation.
Quotient Remainder ----------------------------- 1341/8 = 167 5 --------+ 167/8 = 20 7 ------+ | 20/8 = 2 4 ----+ | | 2/8 = 0 2 --+ | | | (Stop when the quotient is 0) | | | | 2 4 7 5 (OCT; Base 8)Let's express the same decimal number 1341 in hexadecimal notation.
Quotient Remainder ----------------------------- 1341/16 = 83 13 ------+ 83/16 = 5 3 ----+ | 5/16 = 0 5 --+ | | (Stop when the quotient is 0) | | | 5 3 D (HEX; Base 16)
Example. Convert the decimal number 3315 to hexadecimal notation. What about the hexadecimal equivalent of the decimal number 3315.3?
Solution:
Quotient Remainder
-----------------------------
3315/16 = 207 3 ------+
207/16 = 12 15 ----+ |
12/16 = 0 12 --+ | | (Stop when the quotient is 0)
| | |
C F 3 (HEX; Base 16)
(HEX; Base 16)
Product Integer Part 0.4 C C C ...
-------------------------------- | | | |
0.3*16 = 4.8 4 ----+ | | | | |
0.8*16 = 12.8 12 ------+ | | | |
0.8*16 = 12.8 12 --------+ | | |
0.8*16 = 12.8 12 ----------+ | |
: ---------------------+
:
Thus, 3315.3 (DEC) --> CF3.4CCC... (HEX)
Note that from the Base Conversion Table, you can easily get the binary notation from the hexadecimal number by grouping four binary digits per hexadecimal digit, or from or the octal number by grouping three binary digits per octal digit, and vice versa.
HEX 5 3 D BIN 0101 0011 1101 OCT 2 4 7 5 BIN 010 100 111 101Finally, the fractional part in a decimal number can also be converted to any base by repeatedly multiplying the given number by the target base. Example: Convert a decimal number 0.1234 to binary notation
(BIN; Base 2) Product Integer Part 0.0 0 0 1 1 1 1 1 1 0 0 1 ... -------------------------------- | | | | | | | | | | | | | 0.1234*2 = 0.2468 0 ----+ | | | | | | | | | | | | 0.2468*2 = 0.4936 0 ------+ | | | | | | | | | | | 0.4936*2 = 0.9872 0 --------+ | | | | | | | | | | 0.9872*2 = 1.9744 1 ----------+ | | | | | | | | | 0.9744*2 = 1.9488 1 ------------+ | | | | | | | | 0.9488*2 = 1.8976 1 --------------+ | | | | | | | 0.8976*2 = 1.7952 1 ----------------+ | | | | | | 0.7952*2 = 1.5904 1 ------------------+ | | | | | 0.5904*2 = 1.1808 1 --------------------+ | | | | 0.1808*2 = 0.3616 0 ----------------------+ | | | 0.3616*2 = 0.7232 0 ------------------------+ | | 0.7232*2 = 1.4464 1 --------------------------+ | : ----------------------------+ :
Decimal Addition Table: | 0 1 2 3 4 5 6 7 8 9 ---+----------------------------- 0 | 0 1 2 3 4 5 6 7 8 9 1 | 1 2 3 4 5 6 7 8 9 10 2 | 2 3 4 5 6 7 8 9 10 11 3 | 3 4 5 6 7 8 9 10 11 12 4 | 4 5 6 7 8 9 10 11 12 13 5 | 5 6 7 8 9 10 11 12 13 14 6 | 6 7 8 9 10 11 12 13 14 15 7 | 7 8 9 10 11 12 13 14 15 16 8 | 8 9 10 11 12 13 14 15 16 17 9 | 9 10 11 12 13 14 15 16 17 18
Binary Addition Table (equivalent to logical XOR operation with carry): | 0 1 ---+----- 0 | 0 1 1 | 1 10
Octal Addition Table: | 0 1 2 3 4 5 6 7 ---+----------------------- 0 | 0 1 2 3 4 5 6 7 1 | 1 2 3 4 5 6 7 10 2 | 2 3 4 5 6 7 10 11 3 | 3 4 5 6 7 10 11 12 4 | 4 5 6 7 10 11 12 13 5 | 5 6 7 10 11 12 13 14 6 | 6 7 10 11 12 13 14 15 7 | 7 10 11 12 13 14 15 16
Hexadecimal Addition Table: | 0 1 2 3 4 5 6 7 8 9 A B C D E F ---+----------------------------------------------- 0 | 0 1 2 3 4 5 6 7 8 9 A B C D E F 1 | 1 2 3 4 5 6 7 8 9 A B C D E F 10 2 | 2 3 4 5 6 7 8 9 A B C D E F 10 11 3 | 3 4 5 6 7 8 9 A B C D E F 10 11 12 4 | 4 5 6 7 8 9 A B C D E F 10 11 12 13 5 | 5 6 7 8 9 A B C D E F 10 11 12 13 14 6 | 6 7 8 9 A B C D E F 10 11 12 13 14 15 7 | 7 8 9 A B C D E F 10 11 12 13 14 15 16 8 | 8 9 A B C D E F 10 11 12 13 14 15 16 17 9 | 9 A B C D E F 10 11 12 13 14 15 16 17 18 A | A B C D E F 10 11 12 13 14 15 16 17 18 19 B | B C D E F 10 11 12 13 14 15 16 17 18 19 1A C | C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B D | D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C E | E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D F | F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1ENote (in any base, let it be 10, 2, 8, or 16), adding two single digit numbers can lead to the appearance of an additional digit, and that additional digit always has a value of 1.
You can also generate multiplication tables in bases other than 10 by following the same rule you do in base 10.
Decimal Multiplication Table: | 0 1 2 3 4 5 6 7 8 9 ---+----------------------------- 0 | 0 0 0 0 0 0 0 0 0 0 1 | 0 1 2 3 4 5 6 7 8 9 2 | 0 2 4 6 8 10 12 14 16 18 3 | 0 3 6 9 12 15 18 21 24 27 4 | 0 4 8 12 16 20 24 28 32 36 5 | 0 5 10 15 20 25 30 35 40 45 6 | 0 6 12 18 24 30 36 42 48 54 7 | 0 7 14 21 28 35 42 49 56 63 8 | 0 8 16 24 32 40 48 56 64 72 9 | 0 9 18 27 36 45 54 63 72 81
Binary Multiplication Table (equivalent to the logical AND operation): | 0 1 ---+----- 0 | 0 0 1 | 0 1
Octal Multiplication Table: | 0 1 2 3 4 5 6 7 ---+----------------------- 0 | 0 0 0 0 0 0 0 0 1 | 0 1 2 3 4 5 6 7 2 | 0 2 4 6 10 12 14 16 3 | 0 3 6 11 14 17 22 25 4 | 0 4 10 14 20 24 30 34 5 | 0 5 12 17 24 31 36 43 6 | 0 6 14 22 30 36 44 52 7 | 0 7 16 25 34 43 52 61
Hexadecimal Multiplication Table: | 0 1 2 3 4 5 6 7 8 9 A B C D E F ---+----------------------------------------------- 0 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 | 0 1 2 3 4 5 6 7 8 9 A B C D E F 2 | 0 2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E 3 | 0 3 6 9 C F 12 15 18 1B 1E 21 24 27 2A 2D 4 | 0 4 8 C 10 14 18 1C 20 24 28 2C 30 34 38 3C 5 | 0 5 A F 14 19 1E 23 28 2D 32 37 3C 41 46 4B 6 | 0 6 C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A 7 | 0 7 E 15 1C 23 2A 31 38 3F 46 4D 54 5B 62 69 8 | 0 8 10 18 20 28 30 38 40 48 50 58 60 68 70 78 9 | 0 9 12 1B 24 2D 36 3F 48 51 5A 63 6C 75 7E 87 A | 0 A 14 1E 28 32 3C 46 50 5A 64 6E 78 82 8C 96 B | 0 B 16 21 2C 37 42 4D 58 63 6E 79 84 8F 9A A5 C | 0 C 18 24 30 3C 48 54 60 6C 78 84 90 9C A8 B4 D | 0 D 1A 27 34 41 4E 5B 68 75 82 8F 9C A9 B6 C3 E | 0 E 1C 2A 38 46 54 62 70 7E 8C 9A A8 B6 C4 D2 F | 0 F 1E 2D 3C 4B 5A 69 78 87 96 A5 B4 C3 D2 E1
Solution:
From the above decimal addition table, we see that:
3+9=12, 2+8=10, and 1+7=8
123
+ 789
-----
12 <-- 3+9
10 <-- 2+8
+ 8 <-- 1+7
-----
sum 912
Or, we sometimes use the carry notation (depending where we learn our arithmetic)
123
+ 789
-----
carry 11
table + 802
-----
sum 912
Example. Find the sum of two hexadecimal integers 123 and DEF.
Solution:
From the above hexadecimal addition table, we see that:
3+F=12, 2+E=10, and 1+D=E
123
+ DEF
-----
12 <-- 3+F
10 <-- 2+E
+ E <-- 1+D
-----
sum F12
Or, we sometimes use the carry notation (depending where we learn our arithmetic)
123
+ DEF
-----
carry 11
table + E02
-----
sum F12
Example. Find the sum of two binary integers 1 0010 0011 and 1101 1110 1111
Solution:
1 0010 0011
+ 1101 1110 1111
----------------
carry 1 1 11 ... AND operation followed by left-shift
table + 1100 1100 1100 ... XOR operation
----------------
carry 1 1
table + 1110 1000 1010
----------------
carry 1 1
table + 1110 0000 0010
----------------
carry <-- repeat until carry is all 0
sum 1111 0001 0010 (binary)
F 1 2 (hex)
15*256+1*16+2*1=3858 (dec)
A math processor repeats the addition process (i.e., perform AND
& XOR operations) until the carry register is all 0. At that
point, the 2nd register is the sum.
Solution:
Step 1: We break down the second multiplier into single digits.
123*789 = 123*(700+80+9)
= (123*7)*100 + (123*8)*10 + (123*9)
Step 2: We find the product in parentheses, one parenthesis at a time.
From the above decimal multiplication table, we see that:
1*7=7, 2*7=14, 3*7=21; thus,
123*7 = (100+20+3)*7
= 1*7*100 + 2*7*10 + 3*7
= 7*100 + 14*10 + 21
= 700 + 140 + 21
= 861
Likewise,
123*8 = (100+20+3)*8
= 1*8*100 + 2*8*10 + 3*8
= 8*100 + 16*10 + 24
= 800 + 160 + 24
= 984
123*9 = (100+20+3)*9
= 1*9*100 + 2*9*10 + 3*9
= 9*100 + 18*10 + 27
= 900 + 180 + 27
= 1107
Or, in elementary school style:
123 123 123
x 7 x 8 x 9
----- ----- -----
21 24 27
14 16 18
7 8 9
----- ----- -----
861 984 1107
Step 3: We sum up the individual products.
123*789 = (123*7)*100 + (123*8)*10 + (123*9)
= 861*100 + 984*10 + 1107
= 86100 + 9840 + 1107
= 97047
Or, in elementary school style (in the order shown above):
123
x 789
-----
861 <-- 123*7
984 <-- 123*8
1107 <-- 123*9
-----
97047
Or, in elementary school style:
123
x 789
-----
1107 <-- 123*9
984 <-- 123*8
861 <-- 123*7
-----
97047
Example. Find the product of two hexadecimal integers 123 and DEF.
Solution:
Step 1: We break down the second multiplier into single digits.
123*DEF = 123*(D00+E0+F)
= (123*D)*100 + (123*E)*10 + (123*F)
Step 2: We find the product in parentheses.
From the above hexadecimal multiplication table, we see that:
1*D=D, 2*D=1A, 3*D=27; thus,
123*D = (100+20+3)*D
= 1*D*100 + 2*D*10 + 3*D
= D*100 + 1A*10 + 27
= D00 + 1A0 + 27
= EC7
Likewise,
123*E = (100+20+3)*E
= 1*E*100 + 2*E*10 + 3*E
= E*100 + 1C*10 + 2A
= E00 + 1C0 + 2A
= FEA
123*F = (100+20+3)*F
= 1*F*100 + 2*F*10 + 3*F
= F*100 + 1E*10 + 2D
= F00 + 1E0 + 2D
= 110D
Or, in elementary school style:
123 123 123
x D x E x F
----- ----- -----
27 2A 2D
1A 1C 1E
D E F
----- ----- -----
EC7 FEA 110D
Step 3: We sum up the individual products.
123*DEF = (123*D)*100 + (123*E)*10 + (123*F)
= EC7*100 + FEA*10 + 110D
= EC700 + FEA0 + 110D
= FD6AD
Or, in elementary school style (in the order shown above):
123
x DEF
-----
EC7 <-- 123*D
FEA <-- 123*E
110D <-- 123*F
-----
FD6AD
Or, in elementary school style:
123
x DEF
-----
110D <-- 123*F
FEA <-- 123*E
EC7 <-- 123*D
-----
FD6AD
Example. Find the product of two binary integers 1 0010 0011 and 1101 1110 1111
Solution:
Step 2:
1 0010 0011
x 1 (the 1st "1" in 1101 1110 1111)
-------------
1 0010 0011
1 0010 0011
x 1 (the 2nd "1" in 1101 1110 1111)
-------------
1 0010 0011
1 0010 0011
x 0 (the 1st "0" in 1101 1110 1111)
-------------
0 0000 0000
:
:
1 0010 0011
x 1 (the last "1" in 1101 1110 1111)
-------------
1 0010 0011
Step 3: We sum up the individual products.
1 0010 0011
x 1101 1110 1111
--------------------
100100011 <-- 100100011*1
100100011 <-- 100100011*1
000000000 <-- 100100011*0
100100011 <-- 100100011*1
100100011 <-- 100100011*1
100100011 <-- 100100011*1
100100011 <-- 100100011*1
000000000 <-- 100100011*0
100100011 <-- 100100011*1
100100011 <-- 100100011*1
100100011 <-- 100100011*1
100100011 <-- 100100011*1
--------------------
11111101011010101101 (binary, after summing up all the above 12 binary numbers)
F D 6 A D (hex)
Thus, in a computer, multiplication-division becomes a digit shift left-right operation.
Binary Hexadecimal Decimal ------------------------------ 00000000 0 0 00000001 1 1 00000010 2 2 00000011 3 3 00000100 4 4 00000101 5 5 00000110 6 6 00000111 7 7 : : : : : : 11111000 F8 248 11111001 F9 249 11111010 FA 250 11111011 FB 251 11111100 FC 252 11111101 FD 253 11111110 FE 254 11111111 FF 255
Binary Hexadecimal Decimal ------------------------------ 00000000 0 0 00000001 1 1 00000010 2 2 00000011 3 3 00000100 4 4 00000101 5 5 00000110 6 6 00000111 7 7 : : : : : : 01111000 78 120 01111001 79 121 01111010 7A 122 01111011 7B 123 01111100 7C 124 01111101 7D 125 01111110 7E 126 01111111 7F 127 10000000 80 -0 10000001 81 -1 10000010 82 -2 10000011 83 -3 10000100 84 -4 10000101 85 -5 10000110 86 -6 10000111 87 -7 : : : : : : 11111000 F8 -120 11111001 F9 -121 11111010 FA -122 11111011 FB -123 11111100 FC -124 11111101 FD -125 11111110 FE -126 11111111 FF -127
To help understand this scheme, let us start with a decimal machine and consider a 8-digit number display in a tape deck or an odometer in a car. When we roll forward, numbers increment; when we roll backward, numbers decrement. If we were to roll backward past 00000000, all digits simultaneously turn into 9s (e.g., 99999999). If we wish to express both positive and negative numbers, we can agree that the lower half of the meter (00000000 through 49999999, i.e., the leading digit or the most significant digit being 0 through 4) denote positive numbers while the upper half of the meter (50000000 through 99999999, i.e., the leading digit or the most significant digits being 5 through 9) denote negative numbers. With this scheme, with 8 decimal digits, we can express a decimal integer from -50000000 through 49999999.
10's complement (for decimal system) display integer ------------------------------- 50000000 -5000000 50000001 -4999999 50000002 -4999998 50000003 -4999997 50000004 -4999996 : : : : 99999995 -5 99999996 -4 99999997 -3 99999998 -2 99999999 -1 00000000 0 00000001 1 00000002 2 00000003 3 00000004 4 00000005 5 : : : : 49999995 49999995 49999996 49999996 49999997 49999997 49999998 49999998 49999999 49999999Note the symmetry of a positive number (say, 5) and its negative counter part (say, -5) around 0. Thus, subtraction of a number simply becomes addition of its counter part.
5-5 --> becomes --> 5+(-5) --> 0 stored as integer 00000005 5 + 99999995 -5 ---------------------- 100000000 0 (but "1" above is not stored, because the storage has only 8 decimal digits.Another example: 125-125=?
125-125 --> becomes --> 125+(-125) --> 0 stored as integer 00000125 125 + 99999875 -125 ---------------------- 100000000 0The above also suggests a quick way for us mortals to express a negative number in 10's complement notation.
Example: find 10's complement representation of -125 in decimal system. Step 1. positive representation: 00000125 <-- +125 Step 2. find 10's complement 99999874 <-- complement that adds to "9" Step 3. add 1 + 1 --------- The resulting number is -125. 99999875 <-- -125Now, having seen the decimal system, we apply the same idea/procedure to a binary system. With one byte, we first anchor 00000000 to mean zero (0). When we roll forward, numbers increment; when we roll backward, numbers decrement. If we were to roll backward past 00000000, all digits simultaneously turn into 1s (e.g., 11111111). If we wish to express both positive and negative numbers, we can agree that the lower half of the byte (00000000 through 01111111, i.e., the leading digit or the most significant digit being 0) denote positive numbers while the upper half of the byte (10000000 through 11111111, i.e., the leading digit being 1) denote negative numbers. With this scheme, with 8 binary digits (i.e., 1 byte), we can express a decimal integer from -128 through 127.
2's complement (for binary system) display integer ------------------------------- 10000000 -128 10000001 -127 10000010 -126 10000011 -125 10000100 -124 : : : : 11111011 -5 11111100 -4 11111101 -3 11111110 -2 11111111 -1 00000000 0 00000001 1 00000010 2 00000011 3 00000100 4 00000101 5 : : : : 01111011 123 01111100 124 01111101 125 01111110 126 01111111 127Note that just like in signed integer notation, in two's complement notation, a negative number is indicated by "1" in the most significant bit. If we consider this to be the sign bit and also associated with it a value of -128, then, we can decompose the above table like the following.
The most significant bit has a value of -128. display integer ------------------------------------ 10000000 -128 + 0 --> -128 10000001 -128 + 1 --> -127 10000010 -128 + 2 --> -126 10000011 -128 + 3 --> -125 10000100 -128 + 4 --> -124 : : : : 11111011 -128 + 123 --> -5 11111100 -128 + 124 --> -4 11111101 -128 + 125 --> -3 11111110 -128 + 126 --> -2 11111111 -128 + 127 --> -1 00000000 0 + 0 --> 0 00000001 0 + 1 --> 1 00000010 0 + 2 --> 2 00000011 0 + 3 --> 3 00000100 0 + 4 --> 4 00000101 0 + 5 --> 5 : : : : 01111011 0 + 123 --> 123 01111100 0 + 124 --> 124 01111101 0 + 125 --> 125 01111110 0 + 126 --> 126 01111111 0 + 127 --> 127Just as in the decimal system, subtraction of a binary number becomes addition of its counter part.
5-5 --> becomes > 5+(-5) > 0 stored as integer 00000101 5 + 11111011 -5 ---------------------- 100000000 0 (but "1" above is not stored, because the storage has only 8 binary digits.Another example: 125-125=?
125-125 --> becomes > 125+(-125) > 0 stored as integer 01111101 125 + 10000011 -125 ---------------------- 100000000 0The above also suggests a quick way for us mortals to express a negative number in 2's complement notation in binary system.
Example: find 2's complement representation of -125 in binary system. Step 1. positive representation: 01111101 <-- +125 Step 2. find 2's complement 10000010 <-- complement that adds to "1", i.e., flip digits Step 3. add 1 + 1 --------- The resulting number is -125. 10000011 <-- -125A even quicker way to express a negative number in 2's complement notation in binary system is consider the most significant bit to have a value of -2^7=-128 (for a 1-byte 8-bit number), or -2^15=-32768 (for a 2-byte 16-bit number), etc.
-125=-128+3 10000011Example. Find the two's complement representation of the decimal number 1341. From the earlier part of this web page, we first find the hexadecimal notation of this number to be 54D (HEX), which in turn is easily converted to the binary notation. This demonstrates how to find a negative number in two's complement notation without resorting to a look-up table.
1341 (DEC) --> 53D (HEX) --> 0101 0011 1101 (BIN) One byte is not large enough to express 1341 --> need 2 bytes. Step 1. positive representation: 0000 0101 0011 1101 <-- +1341 Step 2. find 2's complement 1111 1010 1100 0010 <-- complement that adds to "1", i.e., flip digits Step 3. add 1 + 1 --------------------- The resulting number is -1341. 1111 1010 1100 0011 <-- -1341An even quicker way is to recognize that -1341=-32768+31427
-1341 (DEC) --> -32768 (DEC) + 31427 (DEC) --> -8000 (HEX) + 7AC3 (HEX) --> -8000 (HEX) + 0111 1010 1100 0011 (BIN) answer: 1111 1010 1100 0011
Example. Find 1341-125. The computer simply adds -125.
0000 0101 0011 1101 <-- +1341 in 2-byte storage + 1111 1111 1000 0011 <-- -125 in 2-byte storage --------------------- carry 1 1 1 + 1111 1010 1011 1110 --------------------- carry 1 1 1 + 1111 0000 1011 1100 --------------------- carry 1 1 + 1110 0100 1011 1000 --------------------- carry 1 1 + 1100 0100 1011 0000 --------------------- carry 1 1 + 1000 0100 1010 0000 --------------------- carry 1 1 <-- the first "1" is pushed off, because the storage has only 2 bytes. + 0000 0100 1000 0000 --------------------- carry <-- repeat until carry is all 0; + 0000 0100 1100 0000 --------------------- sum 0000 0100 1100 0000 <-- answer in binary 0 4 C 0 <-- answer in hexadecimal 4*256+12*16+0*1=1216 <-- answer in decimal
# bits function ------------------------------------ 1 sign 8 binary exponents (-126 through 127) 1 bit for exponent sign (1 for +, 0 for -; opposite of signed integer) 7 bits for exponent value (-126 through 127, expressed in two's complement, but shifted by 1) 23 1.0 ≤ mantissa < 2.0 (1.000...000Because the higher order bit is always 1, there is no need to store this.
----------------------------------------------- value sign sign exp mantissa bits 1 1 7 ----------23----------- ----------------------------------------------- 0 0 0 0000000 00000000000000000000000 all 0 gives 0 (the reason for inverted signed integer for exponent) smallest 0 0 0000000 00000000000000000000001 smallest=~2^-126=1.175*10^-38 2^-125 0 0 0000001 00000000000000000000000 2^-124 0 0 0000010 00000000000000000000000 2^-123 0 0 0000011 00000000000000000000000 2^-122 0 0 0000100 00000000000000000000000 : : 2^-3=0.125 0 0 1111100 00000000000000000000000 2^-2=0.25 0 0 1111101 00000000000000000000000 2^-1=0.5 0 0 1111110 00000000000000000000000 2^0 =1 0 0 1111111 00000000000000000000000 2^1 =2 0 1 0000000 00000000000000000000000 2^2 =4 0 1 0000001 00000000000000000000000 2^3 =8 0 1 0000010 00000000000000000000000 2^4 =16 0 1 0000011 00000000000000000000000 : : 2^125 0 1 1111100 00000000000000000000000 2^126 0 1 1111101 00000000000000000000000 2^127 0 1 1111110 00000000000000000000000 largest 0 1 1111110 11111111111111111111111 largest=~2^128=3.40*10^38 inf 0 1 1111111 00000000000000000000000Example. Find the real number expressed by a 4-byte sequence 4BD40000 HEX
HEX 4 B D 4 0 0 0 0 BIN 0 1 0010111 10101000000000000000000 | | | | | | | +-- binary mantissa: 1.10101000000000000000000 | | +---------- binary exponent value: 0010111+1 | +------------ binary exponent sign: + +-------------- number sign: + Binary value: 1.10101x2^10111+1 = 1.10101*2^11000 Decimal value of mantissa: 1.10101 (BIN) --> 1+0.5+0.125+0.03125=1.65625 (DEC) Decimal value of exponent: 2^(16+8)=2^24 (DEC) Decimal value: 1.65625*2^24=2.778726*10^7Note that the number of bits available for the exponent determines the smallest and the largest number that can be expressed. And the number of bits available for the mantissa determines the number of significant digits.
exponent: 7 binary digits --> 2^(-126~+127) --> 10^(-38~+38) mantissa: 23 binary digits --> ~7 decimal digitsFinally, a little math on digits for quick conversion/estimation:
2^BINdigit = 10^DECdigit 2BINdigit = 10DECdigit log (2^BINdigit) = log (10^DECdigit) log (2BINdigit) = log (10DECdigit) BINdigit*log(2) = DECdigit*log(10) BINdigit*0.30 = DECdigit Another approach: 2=10log(2) → 2BINdigit=10log(2)*BINdigit=10DECdigit compare the exponent in 10 → log(2)*BINdigit=DECdigit Use the above factor of 0.3 for quick estimation exponent of 127 in base 2 --> 127*0.3=~38 ... exponent of 38 in base 10 mantissa of 23 binary digits --> 23*0.3=~7 ... 7 significant digits in base 10 2^BINdigit=10^DECdigit --> BINdigit ~ 3.3*DECdigit ... 3.3 or ~3 binary digits = 1 decimal digit 2^BINdigit= 8^OCTdigit --> BINdigit = 3 *OCTdigit ... 3 binary digit ≡ 1 octal digit 2^BINdigit=16^HEXdigit --> BINdigit = 4 *OCTdigit ... 4 binary digit ≡ 1 hexadecimal digit
|