NEGATIVES NUMBERS IN BINARY
AND THE TWO’S COMPLEMENT
Let’s begin this article by understanding how humans and machines communicate.
In the the world of computers communication there are just two states the computer understands, on and off (represented by 1 and 0) and it’s stored in bits. So in order to communicate with machines its necessary to understand the binary format (the base 2 numeral system which uses only two symbols “0 “ and “1”)
The fallowing table shows the equivalent of binary numbers with decimal system. So the 1 means the number will count, and the 0 will not. This is the representation of the number 177
The system in binary works perfectly for unsigned numbers, from 0 to all positives numbers. It depends in the amount of bit to represent the possible amount of combinations. So if I use 4 bits I will have up to 16 possible combinations, and I could write an integer from 0 (0000) to 15 (1111) in the binary system. But what happens with the signed numbers in binary?
There are three ways we can represent a negative number in binary system. All three are valid system, all depends on what a human programmer chooses the meaning of the bits and what we want to do with them.
1. Signed bit
The first method we are going to see is the signed bit. So if we know the amount of bits we are going to be working with, we can use the last bit from right to left to represent the sing minus. For example the number (-5) here is using just four bits to be represented and the four bit its the minus sign representation.
So with this method, its very easy for humans to represent and understand the negative numbers, however it doesn’t work very well for computers specially for performing operation such as addition or subtraction. Let’s see an example of addition of (5) + (-5)
As we know, this answer is not the one we are expecting, and this is because we have the number [+0] as positive but also a representation of [-0] as negative. As you can see using this method is not useful for this type of operation. so let’s see the other method.
2. One’s complement
This method consist in flipping all the bits of the positive number and then you’ll have the negative one. All the 0’s will became 1’s and all the 1's will became 0’s. Here we still have the same problem of the the number 0 as in the signed bit method, with zero having a negative version . Also operating with this system will be complicated, getting an answer we definitely don’t want. let’s do an example with number (5) + (-5).
And again the answer is not the one we are expecting but this is a lot closer to the one we expect which is zero. So finally we go to the last method which is the two’s complement.
3. Two’s complement
It’s basically doing the same method of ones complements but the difference will be adding the number 1 to that flipped number. With this method we are resolving the problem of having two zero values to just a unique value of zero
To be clear as the numbers are stored in four bits, any carry over is discarded by the machine (1 highlighted). So now let’s s see an example operating with this method.
Conclusion
Two’s complement is an amazing way to stored signed values in memory and works perfectly doing addition and subtracting operations. However we need to be careful with specific cases such as adding two numbers that are two big with the amount of bits we are using.
This clearly can lead us to overflows, and that is’t exactly something we need to avoid. To detect overflows in a two’s complement sum you can check here some rules.
I hoped you enjoyed this article and was useful to step a little further into the wonderful world of knowledge.