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.

0000  0100  1000  1100
0001  0101  1001  1101
0010  0110  1010  1110
0011  0111  1011  1111

Here's a list of the answers I've come up with.

Unsigned

The simple binary numbers 0 through 15.

00000 10008
00011 10019
00102 101010
00113 101111
01004 110012
01015 110113
01106 111014
01117 111115

Two's complement

We get the numbers 7 to -8.

00000 1000-8
00011 1001-7
00102 1010-6
00113 1011-5
01004 1100-4
01015 1101-3
01106 1110-2
01117 1111-1

One's complement

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.

00000 1000-7
00011 1001-6
00102 1010-5
00113 1011-4
01004 1100-3
01015 1101-2
01106 1110-1
01117 1111-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.

00000 1000-0
00011 1001-1
00102 1010-2
00113 1011-3
01004 1100-4
01015 1101-5
01106 1110-6
01117 1111-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.

00000 10008
00011 10019
00102 1010illegal
00113 1011illegal
01004 1100illegal
01015 1101illegal
01106 1110illegal
01117 1111illegal

3 bit unsigned with parity

We can make the first bit a parity bit. Parity is a very simple error checking system. When have even parity, an even number of bits are set to 1. When you have odd parity an odd number of bits are set.

If we use 3 bit unsigned with even parity we get the numbers 0-7 and 8 other bit patterns which contain a parity error.

00000 1000illegal
0001illegal 10011
0010illegal 10102
00113 1011illegal
0100illegal 11004
01015 1101illegal
01106 1110illegal
0111illegal 11117

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.

00000 10004
00010.5 10014.5
00101 10105
00111.5 10115.5
01002 11006
01012.5 11016.5
01103 11107
01113.5 11117.5

And with a fixed binary point at the second position:

00000 10002
00010.25 10012.25
00100.5 10102.5
00110.75 10112.75
01001 11003
01011.25 11013.25
01101.5 11103.5
01111.75 11113.75

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.