This post assumes familiarity with “Two’s Complement”, “One’s Complement” and an understanding of “Positional Numeral Systems”.
Why is the Two’s Complement called the Two’s Complement?
Ever wondered why the two’s complement and the one’s complement are named as such? We were told that to calculate the two’s complement of a number, you add 1 to its one’s complement. But why? When calculating the one’s complement, we simply subtract every digit from 1, so why don’t we subtract every digit from 2 in two’s complement? And maybe, you tried to reason with yourself about how there’s no “2” in the binary system and so that mehtod would not work anyway. There is however a much better way to understand and explain it. To understand this, you need to understand the difference between the radix complement _and the _diminished radix complement.
According to wiktionary:
- The radix complement is the number which, when added to an n-digit number in radix-r, results in r^n. An alternative way of looking at it is that it is the smallest possible (n-1)-digit number in radix-r. The radix complement for radix-r is called r’s complement. We get it by adding 1 to the diminished radix complement.
- The diminished radix complement is the number which, when added to an n-digit number in radix-r results in r^n -1. An alternative way of looking at is is that it is the largest possible n-digit number in radix-r. The diminished radix complement for radix-r is called (r-1)’s complement. We get it by subtracting every digit from (r-1)
In radix-2, the radix complement is the two’s complement and the diminished radix complement is the one’s complement. Let’s try it for an arbitrary byte. The one’s complement of (11001011) is (00110100). If we add them together, we get (11111111) which is the largest 8-digit binary number. The two’s complement however is (00110101). If we add it to (11001011), we get 100000000 which is the smallest 9-digit binary number.
Let’s try the same with the popular and intuitive decimal system. Let’s take 589 as our arbitrary decimal number. The diminished radix complement of 589 aka the nine’s complement would be 410. If we add 589 and 410, we get 999 which is the largest possible 3-digit decimal number. The radix complement aka the ten’s complement would be 411. If we add together 589 and 411 we get 1000 which is the smallest possible 4-digit number.
Just to clear concepts, let’s try ternary(or trinary aka radix-3) as well. The radix complement would be the three’s complement whereas the diminished radix complement would be the two’s complement. Our number is 1021. It’s two’s complement will be 1201. Adding together 1021 and 1201 we get 2222 which is the largest possible 4-digit number in radix-3. It’s three’s complement will be 1202 which, when added to 1021 gives 10000 which is the smallest possible 5-digit number in radix-3.
You must have noticed how the method for calculating the two’s complement in radix-3 was different from the method for calculating the two’s complement in radix-2. Because in binary, the two’s complement is the radix complement whereas in ternary, it is the diminished radix complement. The two share the same name but are completely independent of one another.
Two’s Complement vs Two’s Complement Notation
People, especially CS undergraduates, confuse the two when they are actually two distinct concepts. A junior at my university once told me that she once got a question in an exam in which she was supposed to write the two’s complement of 27 and according to her, it was a trick question because 27 cannot possibly have a two’s complement as two’s complements exist only for negative numbers. I was like what?
Every binary number has a two’s complement. The two’s complement of 1011 is 0101 and the two’s complement of 0101 is 1011. It has nothing to do with negative numbers. Then what is the deal with negatives having two’s complements?
Well, computers have different ways of storing and identifying negative numbers and one of them is called two’s complement notation. In two’s complement notation, for n digits, all numbers between 0 and 10^(n-1) represent positive numbers and all numbers from 10^(n-1) to 10^n -1 represent negative numbers. So all binary numbers that start with a 1 represent negative values of their twos complement. in 4 digits, 0111 represents 7 while 1001, its two’s complement, represents -7.
In binary, 27 is 011011, whose two’s complement is 100101. In two’s complement notation, 27 is still 11011, however -27 will be 100101.