1s Complement
This representation extends the idea from sign-and-magnitude. In sign-and-magnitude, we negate a number by flipping the left-most bit. Extending this negation to flipping the all the bits and not just the left-most bit, we get 1s complement representation.
Similar to sign-and-magnitude, we also need to specify the number of bits we need to use.
Representation
Informally, the idea is to negate a number by flipping all the bits instead of just the left-most bit.
Negative 1s Complement
Flip all the bits
More formally, we can represent 1s complement using the formula:
-x = 2n-x-1
How do we read the formula? Consider a number 12. If we are using 8 bits 1s complement number, we can represent -12 as _the binary representation of -12 = 28-12-1 = 243.
The left part of the formula above
-12 = 28-12-1
comes from the formula above by substituting n = 8 and x = 12. Thus, n is the number of bits and x is the value that we want to negate.
But we did say that the idea is to flip all the bits to negate a number. Let's check if that's correct.
Value | Binary |
---|---|
12 | 00001100 |
243 | 11110011 |
It is as if by magic, the result is indeed as if we flipped all the 8 bits!
Proof
For those who do not believe in magic, here is a simple "proof" with lots of hand-waving.
- 2n in binary is 1 followed by n 0s
- 2n-1 in binary is n 1s
- (2n-1)-x is an XOR operation with binary representation of x since we can now ignore the carry
So, at the end, we XOR all 1s with binary representation of \(x\). Since XOR with 1 is basically a bit flip, that's what we get.
8 bits 1s Complement
Consider 8 bits 1s complement number. How do we represent (-14)10 in this representation?
- Find the binary representation of (14)10: (00001110)2.
- Invert all the bits: (11110001)1s.
- Find the "equivalent" number: -14 = 28-14-1 = 241.
- Find the binary representation of (241)10: (11110001)2.
- Change the base: (11110001)1s.
Quick Quiz
Consider 8 bits 1s complement number. How do we represent (-80)10 in this representation?
(10101111)1s
steps omitted...
Properties
Sign Bit
Since we flip all the bits, it means we also flip the left-most bit. Furthermore, since positive numbers should still be represented as a standard unsigned binary, we find that the left-most bit will actually work similar to a sign bit. This can actually also be shown using the formula, but we omit it.
Range
Given that the left-most bit acts like a sign bit, it means that the largest positive number can be represented as 0111...11. This gives us the same largest value as sign-and-magnitude which is (+2n-1-1)10.
But what about the smallest negative? We at least know that we can flip all the bits from the largest positive to obtain 1000...00. For obvious reason, it is (-2n-1-1)10.
Now, can we actually get a smaller number? Unfortunately, no. Consider the smaller (-2n-1)10. The representation of this in 1s complement should be the bit flip of (+2n-1)10. But (+2n-1)10 is 1000...00. Flipping the bits, we get 0111...11. However, this value is positive!
So, we can conclude that there is no smaller number than (-2n-1-1)10. As such, the range is the same as sign-and-magnitude which is (2n-1)10. Note that we are still missing one number. In fact, we can see that the same problem of redundant 0 is also present in 1s complement.
Kind | Representation | Decimal |
---|---|---|
Largest | 0111...11 | (+2n-1-1)10 |
Smallest | 1000...00 | (-2n-1-1)10 |
Positive 0 | 0000...00 | (+0)10 |
Negative 0 | 1111...11 | (-0)10 |
Arithmetic
The formula for 1s complement actually reveals a very interesting property. Consider any number x. We can show that x + (-x) is equal to 0.
Working
x + (-x)
= x + (2n-x-1)
= 2n-1
= 2n-0-1
= 0
This hints at the possibility that we can perform addition with negative numbers without doing anything special. We can check this by enumerating all possibilities. But that can take long, so we will skip the formal proof.
Addition
Instead, we will show the intuition behind this. We show by starting from the number 0 and keep on incrementing by 1. To simplify the discussion, we keep track of the numbers generated by 4 bit 1s complement.
Value | Binary | Value | Binary |
---|---|---|---|
0 | 0000 | -7 | 1000 |
1 | 0001 | -6 | 1001 |
2 | 0010 | -5 | 1010 |
3 | 0011 | -4 | 1011 |
4 | 0100 | -3 | 1100 |
5 | 0101 | -2 | 1101 |
6 | 0110 | -1 | 1110 |
7 | 0111 | -0 | 1111 |
What is interesting about this pattern is that as we increment the number, we reach the largest number (e.g., +7 with binary 0111) and then we go into the smallest number (e.g., -7 with binary 1000). Ignoring this jump, notice that incrementing the binary by 1 will increment the value by 1 regardless of whether we are in the positive or negative domain.
Extending this further, what is addition except repeated addition by 1! So, it shows that we can actually just perform addition normally and just keep the number of bits in the result to be fixed (i.e., if we start with 4 bits, we should also end with 4 bits).
As we are fixing the number of bits, we can now look at what happen if we increment -0 by 1. Incrementing the binary 1111 by 1, we should technically get 10000. After keeping it at 4 bits, we get 0000. So, incrementing -0, we get +0. As such, we can represent this as a line (or rather, a circle). In this circular representation, an increment is simply following the arrow. So, if we begin at +0 and keep on incrementing, we will get to +7. Incrementing +7 will give us -7.
Now, the circular representation shows a certain problem with performing addition. If we start with a negative number x (e.g., -1) and we add a positive number y such that x + y > 0 (e.g., add 3), then we actually encounter 0 twice! The problem here is that by performing repeated increment, we are actually missing an increment due to redundant 0. So, we need to check if we actually cross this point. If we do, then we add 1 into the result to correct the deficit from redundant 0. Remember, we cross this point if the number of bits in the result should have been 1 more than n.
Overflow
Unfortunately, there can still be a subtle problem with this. Consider the 4 bits 1s complement number again. What happen if we do (7 + 7)?
1 2 3 4 |
|
Notice how 1110
is not actually 14 since we are using 1s complement.
Instead, it is actually -1.
How can two positive numbers result in a negative number?
The problem here is that the result would have been outside of the range of the 1s complement used.
So, we have to check if the result makes sense. This check is called the overflow check. Fortunately, there is a simple rule to check for overflow. First, there are two ways overflow can happen:
- Two positive numbers result in a negative number.
- Two negative numbers result in a positive number.
So, the rule is:
- Check that two operands have the same sign.
- Check that the result have different sign than the operands.
Arithmetic Rule
Combining all the rules, we get the following pseudo-code assuming that both A and B have the same number of bits.
We further assume that the function num_bits
return the actual number of bits.
1s Complement Addition | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Subtraction
We can perform subtraction using 1s complement rather easily because our addition is simply binary addition. The following equation summarises how we can subtract two numbers.
A - B ≡ A + (-B)
Remember, we can simply flip all the bits to obtain -B.
4 bit 1s Complement
1 2 3 4 |
|
1 2 3 4 |
|
1 2 3 4 5 6 7 |
|
1000
has the same sign as the operands.
1 2 3 4 5 6 7 |
|
0101
has different sign from the operands.
Quick Quiz
Calculate the result of the following computation using 4 bits 1s complement. Identify if there is any overflow.
- 4 - 7 = ?
- 3 - -5 = ?
- 7 - 0 =?
-
-3
Steps
- Represent as addition: 4 - 7 = 4 + (-7)
- Convert the number into 1s complement:
- (4)10 = (0100)1s
- (-7)10 = (1000)1s
-
Binary addition
1 2 3 4
0 100 +4 1 000 -7 ----- + --- + 1 100 -3
-
Overflow check:
- The operands have different signs
- There cannot be any overflow
- There is no overflow
-
-0 (overflow)
Steps
- Represent as addition: 3 - -5 = 3 + 5
- Convert the number into 1s complement:
- (3)10 = (0011)1s
- (5)10 = (0101)1s
-
Binary addition
1 2 3 4
0 011 +3 0 101 +5 ----- + --- + 1 000 +8?
-
Overflow check:
- The operands have the same sign
- The result has different sign from operands
- There is an overflow
-
+7
Steps
- Represent as addition: 7 - 0 = 7 + (-0)
- Convert the number into 1s complement:
- (7)10 = (0111)1s
- (-0)10 = (1111)1s
-
Binary addition
1 2 3 4
0 111 +7 1 111 -0 ----- + --- + 10 110 +7
-
Carry check:
1 2 3 4
10 110 --> 0 110 1 ----- + 0 111
-
Overflow check:
- The operands have different signs
- There cannot be any overflow
- There is no overflow