Alternative Binary Representations for 4-bit Signed Integers


The table below shows several alternative 4-bit binary representations for signed integers. Four bits can only represent 16 different numbers, so each encoding attempts to allocate approximately half of the bit patterns for positive and half for negative numbers. Depending on the encoding method chosen, one or both of plus and minus eight may not be representable in the encoding. Some encodings end up with two different encodings for the number zero.

In signed-magnitude representations, the sign bit indicates if the number is positive or negative and the remaining bits indicate the value, e.g. 0101 is +5 and 1101 is -5. This method produces both a plus and minus zero, and in four bits neither plus nor minus eight can be represented. Math only works for positive numbers. (Adding one to a negative number incorrectly makes it more negative!)

In ones-complement representations, all the bits are simply complemented to produce the number of the opposite sign. This method also produces both a plus and minus zero, and in four bits neither plus nor minus eight can be represented. Math works for both all-positive and all-negative numbers, but not if the result has to cross the zeroes. (For example, 6 minus 7 incorrectly gives a result of minus zero, not minus one.)

Twos complement is similar to ones-complement, except that the duplicate zeros have been eliminated by adding one after doing the ones-complement. If you consider zero as a positive number, then twos-complement has an equal number of positive and negative numbers, and you cannot represent the most negative number as a positive number since zero takes up one of the positive bit patterns. Math works for all numbers in the range. You cannot sort twos-complement numbers using an unsigned sort. (If you try, all the negative numbers sort greater than all the positive numbers!)

In biased or excess representations, the encoded value C = V + B, where C is the 4-bit encoded value, V is the signed value to be encoded, and B is the bias to be added to make the encoded value into a positive number that can be encoded using unsigned binary notation. To convert a biased number back to decimal, turn the encoded unsigned binary value into a positive decimal number and then subtract the bias: V = C - B The choice of bias determines where the split lies between positive and negative numbers; even asymmetric splits are possible, e.g. the +7 bias below generates nine positive numbers and only seven negative. Math doesn't work at all for bias/excess encodings, though you can add and subtract unsigned binary numbers from biased numbers correctly throughout their full range. You can sort biased/excess numbers using an unsigned integer sort routine.

Only the twos-complement encoding works with binary addition and subtraction throughout the full range, where adding or subtracting one (or more) gives you the correct answer (as long as you stay in range). Ones-complement math works except around and across zero, and signed-magnitude math only works for positive numbers. As noted above, you can't do any math with biased/excess numbers, unless you stick to adding/subtracting unsigned binary numbers.

Decimal Signed
+8 - - - - 1111
+7 0111 0111 0111 1111 1110
+6 0110 0110 0110 1110 1101
+5 0101 0101 0101 1101 1100
+4 0100 0100 0100 1100 1011
+3 0011 0011 0011 1011 1010
+2 0010 0010 0010 1010 1001
+1 0001 0001 0001 1001 1000
+0 0000 0000 0000 1000 0111
-0 1000 1111 0000 - -
-1 1001 1110 1111 0111 0110
-2 1010 1101 1110 0110 0101
-3 1011 1100 1101 0101 0100
-4 1100 1011 1100 0100 0011
-5 1101 1010 1011 0011 0010
-6 1110 1001 1010 0010 0001
-7 1111 1000 1001 0001 0000
-8 - - 1000 0000 -