Binary Numbers (Handout 914) |
To get the total value of a number, say: 4563, you take each digit and multiply it by the appropriate power of 10. Then add all the products up to get the final value. The following table illustrates this process.
Example Decimal Number: 4563 | ||||
---|---|---|---|---|
Power of 10 | 103 | 102 | 101 | 100 |
Value of Power | 1000 | 100 | 10 | 1 |
Numeral | 4 | 5 | 6 | 3 |
Digit value | 4000 | 500 | 60 | 3 |
4000+500+60+3=4563 |
2N | Binary Number | Decimal Value |
---|---|---|
20 | 1 | 1 |
21 | 10 | 2 |
22 | 100 | 4 |
23 | 1000 | 8 |
24 | 10000 | 16 |
25 | 100000 | 32 |
26 | 1000000 | 64 |
27 | 10000000 | 128 |
28 | 100000000 | 256 |
29 | 1000000000 | 512 |
210 | 10000000000 | 1024 |
... | ... | ... |
216 | 10000000000000000 | 65,536 |
... | ... | ... |
220 | 100000000000000000000 | 1,048,576 |
In programming in a high-level language, we don't usually worry about binary, because when the computer prints out a number for us, it has been translated into decimal. Similarly, when we input a decimal number into the computer, it is translated into binary. Nonetheless, an understanding of how a computer represents values as binary numbers is necessary, both to understand the limitations imposed by calculating using binary numbers and to understand character coding schemes, like ASCII and UNICODE. (Incidentally, if you are interested in the subject of character coding schemes, you are encouraged to follow the following link: "A tutorial on character code issues" at http://www.cs.tut.fi/~jkorpela/chars.html.)
To convert a binary number, say: 1101, to its decimal value, you take each digit and multiply it by the appropriate power of 2. Then add all the products up to get the final value. The following table illustrates this process.
Example Binary Number: 1101 | ||||
---|---|---|---|---|
Power of 2 | 23 | 22 | 21 | 20 |
Value of Power | 8 | 4 | 2 | 1 |
Numeral | 1 | 1 | 0 | 1 |
Digit value | 8 | 4 | 0 | 1 |
8+4+0+1=13 |
The radix (or base) of a number system tells how many different numerals a digit can be in that number system (and consequently determines what powers the positions have.) The decimal system is radix 10; the binary system is radix 2. The radix of a number is sometimes written as a subscript to make clear what sort of a number is being represented. So, in our previous example, we might write "10112 is equal to 1310".
or 4 + 32 + 64 + 128 = 228
228 div 2 = 114 with rem 0 top 114 div 2 = 57 with rem 0 ^ 57 div 2 = 28 with rem 1 | 28 div 2 = 14 with rem 0 | 14 div 2 = 7 with rem 0 | 7 div 2 = 3 with rem 1 | 3 div 2 = 1 with rem 1 | 1 div 2 = 0 with rem 1 bottom
Example:
111 00111010 | 58 +00011100 | +28 =01010110 | =86
Example:
1 00111010 | 58 -00010100 | -20 =00100110 | =38
Just as 3215.1210
equals
(3 × 103) + (2 × 102) + (1 × 101) +
(5 × 100) + (1 × 10-1) + (2 × 10-2)
equals
(3 × 1,000) + (2 × 100) + (1 × 10) + (5 × 1) + (1 × 0.1) + (2 × 0.01)
the binary number 1101.112
equals
(1 × 23) + (1 × 22) + (0 × 21) +
(1 × 20) + (1 × 2-1) + (1 × 2-2)
equals
(1 × 8) + (1 × 4) + (0 × 2) + (1 × 1) + (1 × 0.5) + (1 × 0.25)
equals
13.7510
Each digit to the right of the binary point in a binary fraction
is a multiple of an inverse power of 2.
It is a standard convention to use negative exponents to represent
inverse powers. Thus, for example,
2-1 = 1/(21) = 1/2 = .5
2-2 = 1/(22) = 1/4 = .25
2-3 = 1/(23) = 1/8 = .125
2-4 = 1/(24) = 1/16 = .0625
Hence the binary fraction .11012
represents the decimal fraction .812510 because,
multiplying by the appropriate column power, you get:
(1 × 2-1) +
(1 × 2-2) +
(0 × 2-3) +
(1 × 2-4)
= .812510
Putting whole numbers and fractions together, the decimal number 5.8125 is represented in binary as 101.11012
Using a fixed number of bits to store binary fractions may introduce
rounding errors.
For example, if we had four bits of fractional binary precision,
the decimal fractions that can be represented include:
Binary .0000 is .0000 in decimal.
Binary .0001 is .0625 in decimal.
Binary .0010 is .1250 in decimal.
Binary .0011 is .1875 in decimal.
Binary .0100 is .2500 in decimal.
Binary .0101 is .3125 in decimal.
So if we had the decimal number .05 and we needed to represent it with four binary digits, we simply couldn't do it. Instead we would have to round it to the nearest decimal fraction that can be represented with four binary digits, which is .0625.
Another example:
Assume we wanted to represent the number 5.6 in 8 bits, where the binary
point was assumed to be after the third binary digit from the left.
That is, 8 bits with the format:
ABC.DEFGH
i.e., eight bits with five places to the right of the binary point.
The column weightings are:
A = 22 = 4
B = 21 = 2
C = 20 = 1
D = 2-1 = 1/2 = 0.5
E = 2-2 = 1/4 = 0.25
F = 2-3 = 1/8 = 0.125
G = 2-4 = 1/16 = 0.0625
H = 2-5 = 1/32 = 0.03125
So the answer would be 101.10011 = 5.59375
(Notice the rounding error!)