DEV Community

Andrew Lederman
Andrew Lederman

Posted on • Edited on

Representing Negative Numbers in Binary Notation

We've seen how to add two positive integers together in binary notation. But what happens when we try to subtract?

 0110
-0011
------
010(-1)???
Enter fullscreen mode Exit fullscreen mode

You'll see pretty quickly that we can't subtract 1 from 0 in any given column, so we need a different way to handle subtraction operations in binary notation. There are technically 3 different ways that negative numbers can be represented in binary. The most common is called Two's Complement. But before we get to it, there are two other methods we need to know about...

1.Signed Magnitude

In Base 10, we can represent whether a number is positive or negative by adding the corresponding sign to the left of the number, as in +50 and -45. But we don't have a way to represent the sign directly in binary, we only have bits to work with. Signed means adding the sign (+ or -) to the number, and Magnitude is a term which refers to the actual value of the number. So in the number -65, '-' is the sign, and 65 is the magnitude of the number.

Binary numbers can be signed or unsigned. In a signed number, the left most bit is reserved to represent a positive or negative sign. A 0 in this column represents a positive sign (+) and a 1 represents the negative sign (-). In unsigned, the left most bit simply represents the number in that column. Unsigned numbers can only represent positive numbers and zero. Signed magnitude allows us to represent negative numbers in binary, so this is what we will use for the rest of this post.

10000001 = (-)1 signed
10000001 = 129 unsigned
Enter fullscreen mode Exit fullscreen mode

MSB and LSB

We have seen how the left-most bit of a binary number can be reserved to represent a positive or negative sign. There is actual terminology that describes this position: Most Significant Bit (or MSB for short) describes the left most bit in a binary number. This is useful for describing the signed bit of a signed binary number. The Least Significant Bit (or LSB) describes the right most bit in a number, the ones position. You will come across these terms frequently as you work more with binary numbers.

2. One's Complement

We may be able to represent a sign, but we still aren't able to do an actual subtraction operation. To do this in binary, we need to change our thinking a bit. Instead of subtracting a number, we will add a negative number to the positive. In binary this requires some conversion of the subtrahend (number being subtracted) to make it work.

One's complement simply means to take all the bits of a number, and flip them. If the bit was 0, make it a 1. If it was 1, make it a 0.

0011 = 3
1100 = one's complement of 3
Enter fullscreen mode Exit fullscreen mode

Now we can use this one's complement as the value we will add to our initial number. Let's say we want to subtract 3 from 7. We know that one's complement of 3 in binary is 1100. So we will add this to the binary representation of 7 which is 0111.

  0111 = 7
+ 1100 = one's complement of 3
-------
 10011 = 3?

Enter fullscreen mode Exit fullscreen mode

You can see we are one short of the correct answer which is 4, and our result is currently negative. In One's complement computations if there is a carry bit in the MSB, we do a carry-around by taking the MSB and adding it to the LSB. This will give us the correct answer which is 0100 = 4.

It's important to note that the one's complement of a number is NOT the negative of that number. For instance the one's complement of 3 is 1100, which in binary actually represents the number +4. The complement is just that, a complementary value that when added to our minuend (number to be subtracted from) has the affect of giving us the correct result of a subtraction operation.

One's complement is a method for subtraction which is rarely used in actual computers these days, it is generally used in older systems. It has a shortcoming which is that it has two different representations for zero, positive and negative zero.

0000 = (+)zero
1111 = (-)zero (One's Complement of +zero)

Enter fullscreen mode Exit fullscreen mode

Instead it is most common to see...

3. Two's Complement

Two's Complement is just One's Complement plus one! The only difference is instead of adding 1 after the computation, in Two's Complement we add 1 to our complement before we compute the sum. This is the most common form that a complement will take in binary systems.

Let's subtract 10 from 23 in binary. First we must convert the number to be subtracted into it's Two's Complement:

00001010 = 10 in binary

11110101 = One's Complement of 10

11110110 = Two's Complement of 10
Enter fullscreen mode Exit fullscreen mode

First we take the One's Complement of 10 by flipping each bit in the number, then we add 1 to the LSB to get our Two's Complement. Now we are ready for our subtraction operation.

 00010111 = 23
+11110110 = 10
----------
100001101 = 13    
Enter fullscreen mode Exit fullscreen mode

Since we add the one before the operation, the result is the correct number! In Two's Complement, if our calculation has a carry in the MSB we can truncate it. Our final result is 13.

Conclusion

We've seen how to use Two's Complement to run subtraction operations on two binary numbers. In the next post we will learn about Bitwise Operators, which are a set of operations which gives us a lot more flexibility and greater manipulation of binary numbers!

Top comments (2)

Collapse
 
gilfewster profile image
Gil Fewster

Excellent article. Thank you.

Collapse
 
andrewgl22 profile image
Andrew Lederman

Thanks Gil glad you liked it!