Vocademy

Signed Binary Numbers

So far, we have talked about binary numbers representing positive integers, that is, positive numbers with no decimal point. What about negative numbers?

We could reserve the most significant bit (the leftmost bit or MSB) to indicate the sign. However, then it would take extra steps to handle the sign when doing addition and subtraction. Let's try something different.

Imagine you have an old-time mechanical odometer from a car. Now wind that odometer backward until it reads 000000. What will happen if you wind the odometer back one more kilometer? It will read 999999. We took zero kilometers, subtracted one kilometer, and got 999999 kilometers. It looks like 999999 kilometers is the equivalent of -1 kilometers.

000000
-    1
999999

Winding an odometer backward from zero. This shows that 999999 represents -1.

Wind it back another kilometer, and you get 999998, and so on.

Let's do the same thing with a 1-byte binary number. Let's start with 00000000, and subtract 1. We get 11111111.

00000000
-       1
11111111

0 minus 1 in binary. This shows that a series of 1s can represent -1.

Therefore, using eight bits, +1 is 00000001, and -1 is 11111111. Subtract 1 again, and we get 11111110.

11111111
- 000001
11111110

Negative 1 minus 1 in binary.

This may be a bit confusing to us humans, but it's perfect for storing negative numbers in computers.  Now, addition and subtraction is just like the way we do decimal math. For example, what is 5 - 7? It's -2. Now let's do that with binary numbers. That's 00000101 - 00000111 = 11111110.

  00000101
- 00000111
  11111110

5 - 7 equals -2 or as a signed binary number 11111110.[1]

Let's look at that another way. If we keep subtracting, we can get to -7, which is 11111001. What happens if we add -7 to +5? Adding a negative number is the same as subtracting a positive number. Therefore, +5 + (-7) should also equal -2. Let's try it.

  00000101
+ 11111001
  11111110

+5 plus -7 = -2 or 00000101 + 11111001 = 11111110.

This is convenient for computers for two reasons. First, computers cannot subtract directly. To subtract, they convert the subtrahend to a negative number then add. Second, to convert a positive binary number to a negative number, you just flip all the bits (zeros become ones, and ones become zeros), then add 1. The result is called the two's complement.[2]

  00000111 +7
flip the bits 11111000 one's complement
add 1 11111001 two's complement = -7

To create the two's complement of a binary number, flip the bits then add 1.

Therefore, when we use our simulated odometer to represent negative numbers, negative numbers are represented by the two's complement of the equivalent positive number. This is a perfect fit for computers because it's easy for computers to perform addition and subtraction on numbers represented with that format.

If we use more bits, we just have to increase the number of ones to get a negative number. For example, the 16-bit number 1111111111111111 is also -1. Likewise, 1111111111111110 is -2. You just increase the number of 1s to fill the places you are using.

1111111111111111
-              1
1111111111111110
1 subtracted from -1 using 16-bit numbers

Notice that with this notation, all negative numbers begin with 1.

Now let's go back to 8-bit numbers. We have 11111110 as -2, 11111101 as -3, 11111001 as -7, and so on. If we keep going, we finally reach 10000000, which is -128.[3] What happens if we subtract 1 from -128? If we are using 8-bit numbers, we get 011111111, which represents +127.

10000000       -128
-      1  
01111111      +127
-128 minus 1 in binary. The result is +127.

 Therefore, with signed 8-bit binary numbers, the lowest negative number you can represent is -128. Going the other way, we get something similar. Let's add 1 to +127.

01111111     +127
+      1  
10000000      -128

+127 plus 1 in binary. The result is -128.28.

Therefore, using 8-bit signed binary numbers, the lowest number you can represent is -128 (10000000), and the highest number you can represent is +127 (01111111).

 If you need larger numbers, you just need more bits. Here's the same thing with 16-bit numbers. 0111111111111111 is +33767. If we add 1 we get 1000000000000000, which is -32768.

0111111111111111
+              1
1000000000000000

+32767 plus 1 wraps around, and becomes -32768 if using signed 16-bit numbers.

Therefore, using signed binary integers, negative numbers are represented by the two's complement of the equivalent positive number. As such, the leftmost bit is always 1 for a negative number. Small negative numbers that are represented with a large number of bits will be preceded by a long string of ones (11111101 = -3, so does 1111111111111101). The highest number that can be represented by a signed integer is half that of an unsigned integer. That is because half the possible iterations are used for negative numbers. Another way to look at it is that the first bit represents the sign (0 is positive and 1 is negative), leaving one less bit to represent the magnitude of the numbers.

Biased numbers

There is one problem with using the two's complement to represent negative binary numbers. The microprocessor compare operation doesn't recognize signed integers. For example, let's compare +5 to -7. Using signed integers, +5 is 00000101, and -7 is 11111001. To the compare operation, -7 looks like +249.

To solve this problem, an appropriate bias number is added to each number before the operation. The number added depends on the number of bits used to represent the number. For 8-bit numbers, this is usually 128 or 10000000 in binary. This converts each number into a positive integer for the compare operation. In this example, +5 becomes 133, which is 10000101 in binary. -7 becomes +377 or 101111001. The extra 9th bit to the left is removed, leaving 01111001 or +121. Now the compare operation correctly finds that +5 is greater than -7.

Notice that adding 128 to an 8-bit number simply flips the most significant bit; 00000101 becomes 10000101, and 11111001 becomes 01111001. Numbers biased with 128 are also called excess 128 numbers. For larger numbers, a larger bias number is used. For example, a 16-bit number would be biased with 32768 or 1000000000000000, etc.

—————————
1When subtracting decimal numbers where the result is negative, you swap the menuend and subtrahend, subtract then add a negative sign. Using our simulated odometer notation with signed binary integers you fill the empty places to the left with ones.
2The first step of just flipping the bits is results in the one's complement.
3-128 and +128 (unsigned) are represented by the same group of bits using 8-bit numbers. Using 16-bits -128 is 1111111110000000
Vocademy