Numbers: Unsigned
Numbers: Unsigned
Numbers
Numbers
Here's a interesting question that can be used to judge how much a computer programmer really knows about the machine. Write out the bit patterns from 0000 to 1111 and ask, "How many different numbers can you get from this.
0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1
Unsigned
The simple binary numbers 0 through 15.
0 0 0 00 1 0 0 0
Two's complement
We get the numbers 7 to -8.
0 0 0 00 1 0 0 0-8 0 0 0 11 1 0 0 1-7 0 0 1 02 1 0 1 0-6 0 0 1 13 1 0 1 1-5 0 1 0 04 1 1 0 0-4 0 1 0 15 1 1 0 1-3 0 1 1 06 1 1 1 0-2 0 1 1 17 1 1 1 1-1
One's complement
www.oualline.com/practical.programmer/numbers.html 1/4
2/1/13
Numbers
Some of the older computers actually used 1's complement because it was simpler to design some 1's complement circuits. The problem with 1's complement is the existence of negative 0. The numbers range from 7 to -7 including 0 and -0.
0 0 0 00 1 0 0 0-7 0 0 0 11 1 0 0 1-6 0 0 1 02 1 0 1 0-5 0 0 1 13 1 0 1 1-4 0 1 0 04 1 1 0 0-3 0 1 0 15 1 1 0 1-2 0 1 1 06 1 1 1 0-1 0 1 1 17 1 1 1 1-0
Signed magnitude
We actually used signed magnitude in our everyday life. It's used for normal decimal integers. We have a sign character, followed by a number of digits of magnitude. For our bit patterns, signed magnitude gives number from 7 to -7 with 0 and -0. However, we don't use the same bit patterns as 1's complement.
0 0 0 00 1 0 0 0-0 0 0 0 11 1 0 0 1-1 0 0 1 02 1 0 1 0-2 0 0 1 13 1 0 1 1-3 0 1 0 04 1 1 0 0-4 0 1 0 15 1 1 0 1-5 0 1 1 06 1 1 1 0-6 0 1 1 17 1 1 1 1-7
BCD
Binary Coded Decimal uses 4 bits to represent a single digit. It was used extensively in financial calculations. Things like updating a billing record where the cost of converting to binary was expensive considering that only a few simple operations needed to be performed with the numbers. So using BCD we get a single digit, which leaves us with the numbers 0-9 and 6 illegal bit patterns.
0 0 0 00 1 0 0 0 0 0 0 11 1 0 0 1
9 0 0 1 02 1 0 1 0illegal
www.oualline.com/practical.programmer/numbers.html 2/4
2/1/13
Numbers
2 0 1 1illegal 31 4
1 0 1illegal 51 1 1 0illegal 61
0 1 1 1illegal 1 1 1 1
Gray code
In Gray code only one bit changes at a time. That makes it very useful for position encoders. Straight binary is not very good for this job. Think about what would happen if we used straight binary for an encode and positioned the device exactly on the bounder between 7 and 8. Each bit could be on or off depending on the whims of the sensor.
Fixed point
The 4 bit unsigned representation contains an implied binary point just after the last digit. We can move the binary point to the left one and get the numbers 0.0 through 7.5 by 0.5 increments. We can move it to the left two and get 0.0 through 3.75 by 0.25 increments.
0 0 0 4 01 0 0 0 10.5 1 0 0 14.5 0 0 0 0 0 1 0 5 11 0 0 1 11.5 1 0 1 15.5 0 1 0 0 21 1 0 0 6 0 0 1 0
www.oualline.com/practical.programmer/numbers.html 3/4
2/1/13
Numbers
Floating point
We have enough bits for a sign, exponent and fraction. We could make floating point numbers out of our bit patterns. I think it would interesting to try because almost every bit pattern involves a boundary condition. We'll leave that for another day.
Strange
Finally we can use a system which assigns the following values to our bit patterns: "0", "1", "83", "47", "a fish", "a horse", "a cow", and "Tuesday". Some of you might look at this and think it's strange. Others may look at and think this author is strange. How do you get such a set of values? The answer is simple, I made it up. Ultimately the assignment of meaning to bit patterns is up to the programmer. True some common systems like unsigned binary make life a lot easy for him, but ultimately he can choose any assignment he wants to.
www.oualline.com/practical.programmer/numbers.html
4/4