1 - Computer Systems / 2s Compliment
|
|
2s Complement - Negative Binary
2s Complement - Negative Binary numbers
It is possible to represent negative numbers using the binary numbers system. To do this we must make use of a system called 2s complement.
There are actually other methods of representing negative binary numbers e.g. signed magnitude and 1s complement, however they both have their flaws and therefor 2s complement is most commonly used and that is the system that we will focus on here.
Sign Bit
With 2s complement, the first thing that we should note is that the left most bit (MSB) is reserved as a sign bit. This means that the left most bit cannot be used to represent a number but rather it is used to signify positive or negative.
If the left most bit is 1 then the number is negative
If the left most bit is 0 then the number is positive
Below you can see an incomplete table demonstrating the complete binary numbers from 0 - 7 plus the implementation of sign bits for all numbers from -7 to 7:
If the left most bit is 1 then the number is negative
If the left most bit is 0 then the number is positive
Below you can see an incomplete table demonstrating the complete binary numbers from 0 - 7 plus the implementation of sign bits for all numbers from -7 to 7:
Now that you understand the idea of the sign bit we can begin to explore the rest of the process that will allow us to complete this table.
The process itself is quite simple, you only really need to remember 2 steps, these are:
The process itself is quite simple, you only really need to remember 2 steps, these are:
Step 1 - Flip the bits
Step 2 - Add 1
Converting positive into negative
So now we wish to implement this we should try to do so first with the number -1.
Take the positive binary for 1 which (if using the 4 bit system above) is 0001.
Step 1 - Change the sign bit to 1:
This would leave us with 1001
Step 2 - Flip the bits. - This literally means change the 1s to 0 and the 0s to 1. This should be applied to all bits except the sign bit:
This would leave us with 1110
Step 3 - Add 1 to the binary number 1110:
This would leave us with 1111 and therefor -1 in binary 2s compliment is 1111
Now if we follow this system we can complete our table and it will look something like this:
Take the positive binary for 1 which (if using the 4 bit system above) is 0001.
Step 1 - Change the sign bit to 1:
This would leave us with 1001
Step 2 - Flip the bits. - This literally means change the 1s to 0 and the 0s to 1. This should be applied to all bits except the sign bit:
This would leave us with 1110
Step 3 - Add 1 to the binary number 1110:
This would leave us with 1111 and therefor -1 in binary 2s compliment is 1111
Now if we follow this system we can complete our table and it will look something like this:
Positive to Negative conversion example
Lets work through an example of how you might apply this to a larger number.
If you were asked to work out the negative 2s complement binary for the number 71 (Or any number) then you should follow these steps
The first step is to work out what the binary is for positive 71:
Sign |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
O |
1 |
O |
O |
O |
1 |
1 |
1 |
The next step is to "Flip the bits". This literally means to change every 0 into a 1 and every 1 into a 0. See below example.
1 |
O |
1 |
1 |
1 |
O |
O |
O |
There is only one step left to complete the process and that is to add 1. We can achieve this by following the binary addition rules.
Below you can see 1 being added to 10111000:
Below you can see 1 being added to 10111000:
Our final result here is 10111001 - This binary number represents -71 when using the 2s complement system.
Negative to Positive Conversion Example
Converting a negative 2s complement binary number into a positive one can be achieved by following these steps.
Step 1 - Flip the sign bit from a 1 to a 0
Step 2 - Flip all of the bits i.e. 0 to 1 and 1 to 0
Step 3 - Add 1
The result will be the positive binary representation. Once you have this you can then perform a binary to denary conversion should you require the denary value.
Example:
Convert the 2s complement binary number 10101001 (-87) into a positive denary number.
Step 1 - Flip the sign bit from a 1 into a 0:
Step 1 - Flip the sign bit from a 1 to a 0
Step 2 - Flip all of the bits i.e. 0 to 1 and 1 to 0
Step 3 - Add 1
The result will be the positive binary representation. Once you have this you can then perform a binary to denary conversion should you require the denary value.
Example:
Convert the 2s complement binary number 10101001 (-87) into a positive denary number.
Step 1 - Flip the sign bit from a 1 into a 0:
O |
O |
1 |
O |
1 |
O |
O |
1 |
Step 2 - Flip the remaining bits:
O |
1 |
O |
1 |
O |
1 |
1 |
O |
Step 3 - Add 1:
We now have the positive 2s complement binary number - 01010111
As we were tasked with returning the positive denary number we should now perform a binary to denary conversion as demonstrated below:
As we were tasked with returning the positive denary number we should now perform a binary to denary conversion as demonstrated below:
Sign |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
O |
1 |
O |
1 |
O |
1 |
1 |
1 |
64 + 16 + 4 + 2 + 1 = 87
We have successfully converted a negative 2s complement number into a positive denary number!
We have successfully converted a negative 2s complement number into a positive denary number!
Here is an excellent YouTube video which very nicely sums up the signed magnitude, 1s complement and 2s complement system.
You are only required to know 2s complement for your CAIE IGCSE, however this should help to provide a deeper understanding.
You are only required to know 2s complement for your CAIE IGCSE, however this should help to provide a deeper understanding.