b) 1’s Complement
If we remap our negative numbers from top-down, then we will be getting following binary table:
S. No. | Binary Representation | Decimal Value | |
1’s complement | Signed bit | ||
A | 0000 | +0 | +0 |
B | 0001 | +1 | +1 |
C | 0010 | +2 | +2 |
D | 0011 | +3 | +3 |
E | 0100 | +4 | +4 |
F | 0101 | +5 | +5 |
G | 0110 | +6 | +6 |
H | 0111 | +7 | +7 |
I | 1000 | -7 | -0 |
J | 1001 | -6 | -1 |
K | 1010 | -5 | -2 |
L | 1011 | -4 | -3 |
M | 1100 | -3 | -4 |
N | 1101 | -2 | -5 |
O | 1110 | -1 | -6 |
P | 1111 | -0 | -7 |
How to get binary representation of an integer in 1’s complement method?
- Positive numbers are represented similar to signed integer method
- Negative numbers are represented by inverting every bit of corresponding positive number (inverting can easily be done by using NOT gate during hardware design)
Let’s analyze this closely to see if we have achieved some improvement.
1) Two representations of zero:
In this approach also we have two representations of zero.
2) Signed extension doesn’t work for negative numbers:
Signed extension works perfectly for negative numbers.
Decimal | 4-bit | 5-bit | 6-bit |
+2 | 0010 | 00010 | 000010 |
+7 | 0111 | 00111 | 000111 |
-2 | 1101 | 11101 | 111101 |
-7 | 1000 | 11000 | 111000 |
3) Binary addition works with modified rules:
Binary | Decimal | Binary | Decimal | Binary | Decimal | |||
Number-1 | 0010 | +2 | 0111 | +7 | 1010 | -5 | ||
Number-2 | 1101 | -2 | 1101 | -2 | 0011 | +3 | ||
Binary addition | 1111 | -0 | 0100 | +4 | 1101 | -2 | ||
Decimal addition | +0 | +5 | -2 |
The answer is not always correct, but it is very close to the right answer. We can make it work if we follow the rule that if you have generated carry forward on your left most bit, then don’t throw it away instead bring it back and add it to the right most bit.
Binary | Decimal | Binary | Decimal | Binary | Decimal | |||
Number-1 | 0111 | +7 | 1110 | -1 | 0111 | +7 | ||
Number-2 | 1101 | -2 | 1001 | -6 | 1011 | -4 | ||
Binary addition | (1) 0100 | +4 | (1) 0111 | +7 | (1) 0010 | +2 | ||
Adding carry forward back | 0101 | +5 | 1000 | -7 | 0011 | +3 |
Definitely 1’s complement method is better than signed bit. Our major concerns are resolved but remain issue (having two representations of zero) and our hack in binary addition give clues to improve 1’s complement method. Let’s rephrase those sentences to make it easier.
- We have an extra representation of zero which is unnecessary
- While addition two binary numbers, if we have a carry forward in left most bit, then we have to add +1 to the result i.e., the right answer can be found by traversing down to next row in the binary table.
Both of them direct us that an extra representation of zero is the root cause of issue. So, let’s remove this extra zero and shift all negative values to the next row (-7 will move from I -> J, -6 will move from J -> K and so on…)
Two’s Complement
There are three different ways to represent signed integer (article). a: Signed bit, b: 1’s Complement, and c: 2’s Complement. Let’s try to understand how these methods have derived and why 2’s complement is preferred over others.
As we know that data are stored in bits. How can we store signed integer in the memory? To solve this problem, first we will develop a naïve solution and then will iterate it till we have the best solution for our problem.