In my last post about binary signed integers, I introduced the ones complement representation. At the time, I said that the ones complement was found by taking the bitwise complement of the number. My explanation about how to do this was simple: invert each bit, flipping 1 to 0 and vice versa. While it’s true that this is all you need to know in order to determine the ones complement of a binary number, if you want to understand how computers do arithmetic with signed integers and why they represent them the way they do, then you need to understand what complements are and how the method of complements allows computers to subtract one integer from another, or add a positive and negative integer, by doing addition with only positive integers.

The radix of a number system, also known as a number system’s base, is the number of unique digits, including zero, that are used to represent the system’s numbers. The decimal number system consisting of digits 0 – 9 has a radix of 10. The binary number system, which we are interested in here, has two digits: 0 and 1 so its radix is 2. One other number system that plays an important role in computer systems in hexadecimal. Hexadecimal numbers are base 16. In addition to the digits 0 – 9, the system also includes the digits A, B, C, D, E, and F to represent the decimal numbers 10, 11, 12, 13, 14, and 15 respectively.

In instances where the radix of a number is ambiguous, it will be denoted as a subscript to the right of the number. For example, is decimal number 11 while is binary 11 (decimal number 3).

The radix complement of an n-digit number in radix b is defined to be:

So what does that make the radix complement? Let’s think about 2 digit decimal numbers. Since we are dealing in radix (base) 10, then equals 100, the smallest 3 digit decimal number. The radix complement, therefore, is the number that when added to y will cause the sum to “rollover” from 2 digits to three digits.

This is true regardless of the radix. If we consider a three digit binary number, say , then equals . The radix complement is calculated thusly:

So the radix complement of is .

The radix complement of decimal number is known as the tens complement, while the radix complement of a binary number is called the twos complement. Be sure to remember the latter!

There is an easier way to calculate the radix complenment, but to do so you first need to know about the diminished radix complement.

The diminished radix complement of an n-digit number y in radix b is defined as:

We see that the diminished radix complement is just one less than the radix complement. It is the number that when added to yields the largest number that can be represented by n digits in radix b. The diminished radix complement of a decimal number is called the nines complement while the diminished radix complement of a binary number is called the ones complement. Sound familiar?

So why bother? Why give this number it’s own name when we’ve already got the radix complement? The answer is because by its nature, the diminished radix complement is much easier to compute than the radix complement. Once you’ve got the diminished radix complement all you need to do is add 1 to it to get the radix complement:

How easy is it to calculate the diminished radix complement? Simple. Subtract every digit in your number from (b – 1) and you’re done. Easy peasy.

For example, to find the nines complement of 487341 just subtract each digit from 9, and you’ve got your answer: 512658. The tens complement? To calculate it just add 1, and you get 512659. We can do a quick sanity check by adding our original number to the tens compliment. The tens complement of six digit decimal (radix 10) number should be or 1000000.

And it is. We’re sane.  Or I am anyway. I can’t speak for you.

Now we can understand why taking the bitwise complement of a binary number yields the ones compliment, ie., the diminished radix complement. Flipping each bit from 0 to 1 and vice versa is the same as subtracting each bit value from 1 (), our shortcut for calculating the diminished radix complement. and .

But why does that work? Why can we subtract every digit from to get the diminished radix complement? That can be explained with a little algebra.

Consider:

This is equivalent to:

This is binomial that can be factored according to the rule:

Applying this rule to our binomial:

The answer is in there, but we can tease it out to make it a little more obvious. Let’s expand the summation:

Now multiply through:

Hopefully it is obvious now. are the respective place values of an n-digit number with radix b. Multiplying each place value by b – 1 gives us an n-digit number where each digit is b – 1.  So, if every digit of is b – 1, then subtracting is the same as subtracting every digit of y from b  – 1.

That wraps up this introduction to complements. In the next post I am going to discuss the method of complements, a method use to subtract one number from another using the addition of only positive numbers. It is this principle that underlies the way computers perform addition and subtraction with binary signed integers. Until then.