Complements

The complement of a number is a number which when added to the original will make it equal to a multiple of the base of the number system.

Generally the base is represented by ‘r’. so we cancategorized complement in the term of ‘r’

1. r’s complement

2. (r-1)’s complement

General complement table of normally used bases:

No.System ‘r’ r’s complement

(r-1)’s complement

Binary 2 2’s complement

1’s complement

Octal 8 8’s complement

7’s complement

Decimal 10 10’s complement

9’s complement

Hexadecimal 16 16’s complement

15’s complement

Rules:

• Farmula for r’s complement: rn -N

r = base

N = given +ve number

n = the number of digits in the integer part of N

• Farmula for (r-1)’s complement: rn – r -m - N

m = the number of digits in the fractional part

Example: Find 10’s complement of (1789)10

Sol: here r = 10 , n = 4 , N = (1789)10

= rn –N = (104)10 -(1789)10

= 10000 – 01789 = (8211)10

Example: Find r’s complement of (998)16

Sol: here r = 16 , n = 3 , N = (998)16

= rn –N = (163)10 -(998)16

= (4096)10 -(998)16

= (1000)16 -(998)16 = (0002)16

Example: Find 9’s complement of (1789)10

Sol: here r = 10 , m=0, n = 4 , N = (1789)10

= : rn – r -m - N = ( rn – r 0 )- N = (rn – 1)10 – N

=( 104-1 )10 -(1789)10

= 10000 –1- 01789 = (8210)10

Note: If there is no fractional value then the farmula= (rn – 1)10 – N

Example: Find ( r-1)’s complement of (111.10)10

Sol: here r = 2 , n = 3 ,m = 2, N = (111.10)16

= rn – r -m - N = ( 23 – 2 -2 )^10 - N = (8 – .25)10 – N = (7.75)10 -(111.10)2

= (111.11)2 -(111.10)2

= (000.01)2

Signed numbers:

Sign Complement form -----*1’s complement * 2’s complement form

Sign magnitude form

Ones' complement

In ones' complement, the inverse of a number is formed by complementing each bit — that is, performing a bitwise NOT operation. For example:

0101 = +5

1010 = -5

A side effect of both this and the previous system, one of the reasons these systems are not often used for computing, is that there are two representations for zero. In ones' complement:

0000 = +0

1111 = -0

In sign-magnitude:

0000 = +0

1000 = -0

Rules for 1’s COMPLEMENT of a binary number

Under this method first we have to find out the given number’s sign

1. I f the number is +ve, then the binary equivalent is the complement

2. If the number is –ve, then ,

Step 1 – change in binary equivalent

Step- 2 - REPLACE 0 BY 1 AND 1 BY 0

Ex. (+77)10

Step:1-------Binary equivalent of 77 = (1001101)2

Step:2-------It’s 1’s Complement = 1001101

Step:--------Given number’s sign is +ve , so the answer: 1001101

Note: If the answer is required in 16 bit buffer then the answer is 0000000001001101

Ex. (-77)10

Step:1-------Binary equivalent of 77 = (1001101)2

Step:2-------Sign is –ve so, It’s 1’s Complement = 0110010

Note: If the answer is required in 16 bit buffer then the answer is 1111111110110010

2’s COMPLEMENT

Two's complement is the most widely used system in modern computing. To form the two's complement, take the bitwise NOT of the number and add 1. For example:

0101 = +5

1011 = -5

Thus:

0000 = decimal 0 1000 = decimal -8

0001 = decimal 1 1001 = decimal -7

0010 = decimal 2 1010 = decimal -6

0011 = decimal 3 1011 = decimal -5

0100 = decimal 4 1100 = decimal -4

0101 = decimal 5 1101 = decimal -3

0110 = decimal 6 1110 = decimal -2

0111 = decimal 7 1111 = decimal -1

Using this system, 16 bits will encode numbers from −32,768 to 32,767, while 32 bits will encode −2,147,483,648 to 2,147,483,647.

The great advantage of the two's complement system is that most operations are not dependent on the sign of the operands and furthermore are identical to operations on unsigned binary integers.

For instance, 5 + (-5) would be performed like this:

0101

+1011

10000

However, seeing as we have taken the numbers as 4 bits long, the leading 1 is discarded and we have the expected result of 0.

The fact that most operations work no matter the sign of the operands can be explained through the duplicity of numbers modulo 2n; e.g. 15 ≡ -1 (mod 16). Computers generally use a fixed number of bits for binary numbers and thus such a system is ideal. Essentially the only difference between two's complement numbers and unsigned numbers is how they are displayed and compared.

One quirk of two's complement is that the lowest encodable number (e.g. -32768 for 16 bit numbers) appears to be its own negative. However, this rarely causes problems.

As a number composed entirely of 1s (such as 11111111) equates to -1 in two's complement notation, many programming languages use -1 for true and 0 for false.

Rules for 2’s COMPLEMENT of a binary number

Under this method first we have to find out the given number’s sign

3. I f the number is +ve, then the binary equivalent is the complement

4. If the number is –ve, then ,

Step 1 – change in binary equivalent

Step- 2 – find the complement by replacing 0 by 1 and 1 by 0

Step-3- add 1 In step one’s result

Ex. (+77)10

Step:1-------Binary equivalent of 77 = (1001101)2

Step:2-------It’s 1’s Complement = 1001101 = It’s 2’s Complement

Step:3--------Given number’s sign is +ve , so the answer: 1001101

Note: If the answer is required in 16 bit buffer then the answer is 0000000001001101

Ex. (-77)10

Step:1-------Binary equivalent of 77 = (1001101)2

Step:2-------Sign is –ve so, It’s Conversion = 0110010

Step-3: ----- 0110010 + 000001 = 0110011

It’s 2’s Complement = 0110011

Note: If the answer is required in 16 bit buffer then the answer is 1111111110110011

Practice: change these decimal numbers in binary one’s complement & 2’s complement & compare them—99, 1786 ,786

## 07 October 2010

Subscribe to:
Post Comments (Atom)

## No comments:

## Post a Comment