**Prerequisite – **Complement of a Number

The use of complements is to mainly perform **Subtraction.** We can easily perform addition in contrast if we would like to implement subtraction using logic gates we require more (because we need to consider borrowing that kind of stuff). So if we somehow make it into simple expressions i.e. we are using less expensive circuits to implement the operations.

**There are basically 2 types of complements for each base-r system:**

**1. Diminished Radix Complement ((r-1)’s Complement):**

If we are given a number N in base-r having n digits the (r-1)’s complement or Diminished Radix complement is defined as –

(r^n - 1) - N

**Example 1:**

Let us take r = 10 i.e. we need to find 9’s complement of a number. Let the number be N, then 9’s complement of N is

10^n -1 -N i.e. 999....99 (n digits) - N

**Example 2:**

Let us take N =1988. Here, r = 10 and n = 4, so 9’s complement of 1988 is

9999 - 1998 = 8001

**Example 3:**

Let us take N = 01234. Here, r = 10 and n = 5, so 9’s complement of 01234 is

99999 - 01234 = 98765

- For binary numbers, r = 2
- So 2^n is represented as 100…00 (1 followed by n 0’s)
- This implies 2^n -1 is represented as 1…11(n 1’s)
- In the case of binary 1’s complement, we see that it has all 1’s i.e. 1-1=0 and 1-0=1
- This means if we just change 1 from 0 and 0 from 1, we directly get its complement.

**Example 4:**

Let us take N = 010010 and r = 2 and n = 6. So directly 1’s complement of N is

111111-010010 = 101101

### 2. Radix Complement (r’s Complement):

If we are given a number N in base-r having n digits the (r)’s Complement or Radix Complement is defined as:

r^{n}- N

This is also the same as adding 1 to (r-1)’s compliment to get r’s complement.

**Example 1:**

Let N = 12345 and n = 5 and r = 10. So 10’s complement of N is

100000 - 12345 = 98765

Let N = 010101, n = 6 and r = 2

So 2’s complement is 1’s complement +1 i.e. change 1 to 0 and 0 to 1 and then add 1 to the number. So 2’s complement of 010101 is

101010+1 = 101011

We can also do the above as 2’s complement can be formed by leaving all least significant 0’s and the first 1 unchanged and replacing 1’s with 0’s and 0’s with 1’s in all other higher significant digits.

**Example 2: **The 2’s complement of 0110111 is 1001001

**Performing Subtraction using r’s and r-1’s Complement: **When subtraction is implemented with digital hardware, the **borrowing **method is less efficient than the method that uses complements. (More number of circuits are required). One of the things to keep in mind before doing subtraction is to make sure that both have the same number of digits.

The subtraction of two n‐digit unsigned numbers M – N in base-r can be done as follows –

**Example 1:**

- Using 10’s complement find 98765 – 4321
- So the above also means that find the value of 98765 – 04321
- Let us calculate 10’s complement of 04321 is

100000 - 04321 = 95679

Add 10’s complement of N to M 98765 + 95679 = 194444 we got a carry of 1 which means that M-N is positive. So just remove the carry if there is any the resulting will be the answer so the answer id 94444.

**Example 2:**

- Using 10’s complement find 4321-98765 we need to calculate 04321-98765
- So first calculate 10’s complement of 98765
- 10’s complement of 98765 is

100000 - 98765 = 01235 So, 01235 + 04321 = 05556

Since there is no carry (i.e. most significant digit is 1) the number is negative now we need to again take 10’s complement and with a minus sign in front of 05556, i.e.

-(100000 - 05556) = -94446

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the **CS Theory Course** at a student-friendly price and become industry ready.