Wednesday, December 26, 2012

2's complement



In mathematics, negative numbers in any base are represented by prefixing them with a − sign (negative sign). However, in computer hardware, numbers are represented in binary without any extra symbols, requiring a method of encoding the minus sign. In computing, 2's complement is one of the method to represent the negative numbers. 2's complement is dominant today. We can also represent negative numbers using 1's complement. 

Click here to know about 1's complement.

Methods to find 2's complement

Arithmetic Method

We can find 1's complement by using the formula below:
R = 2n -N
where n = number of bits per word
          N is a positive integer for which we have to find 1's complement
          R is -N in 2's complement notation

Example 1:  With an 8-bit word and N = 9, we have:
                            R = 28  - 9 = 256 - 9 = 247 = 1111 0111


Binary Method

To find the 2's complement: Start from right side of the binary number and complement each bit to the left of the first "1"
For example: 
N = +6 = 0 0 0 0 0 1 1 0
R = -6  = 1 1 1 1 1 0 1 0

another example
N = +9 = 0 0 0 0 1 0 0 1
R = -9  = 1 1 1 1 0 1 1 1 




Note: Given 2's complement we can find the magnitude of a number by taking its 2's complement.

r's complement
Formula to find (r-1)'s complement, where r is the base or radix (i.e. for decimal numbers  r =10, for binary r =2, for hexadecimal number r =16, for octal number r =8)

r's complement of N = (rn - N)
where r = base
          n = number of digits in the integer part of N

No comments:

Post a Comment