Sunday, 21 October 2012

complement



01’s Complement Form
Definition:
A system used in some computers to represent negative numbers. To negate a number, each bit of the number is inverted (zeros are replaced with ones and vice versa). This has the consequence that there are two representations for zero, either all zeros or all ones.
Number representation
Positive numbers are the same simple, binary system used by two's complement and sign-magnitude. Negative values are the bit complement of the corresponding positive value. The largest positive value is characterized by the sign (high-order) bit being off (0) and all other bits being on (1). The smallest negative value is characterized by the sign bit being 1, and all other bits being 0. The table below shows all possible values in a 4-bit system, from −7 to +7.
       +       -
 0   0000   1111   —Note that +0 and −0 return TRUE when tested for
                                        zero , FALSE when tested for non-zero.
 1   0001   1110
 2   0010   1101
 3   0011   1100
 4   0100   1011
 5   0101   1010
 6   0110   1001
 7   0111   1000

Sign-Magnitude Form
Another method of representing negative numbers is sign-magnitude. Sign-magnitude representation also uses the most significant bit of the number to indicate the sign. A negative number is the 7-bit binary representation of the positive number with the most significant bit set to one. The drawbacks to using this method for arithmetic computation are that a different set of rules are required and that zero can have two representations (+0, 0000 0000 and -0, 1000 0000).
For example:
1.   In this form, a negative number is the 1’s complement of the corresponding positive number
2.     The 1’s complement of a binary number is found by changing all 1s to 0s and all 0s to 1s as shown below:
+2510 = 000110012
-2510= 111001102(1’s complement of +25)
2’s Complement Form
Definition
Property
Two's complement representation allows the use of binary arithmetic operations on signed integers, yielding the correct 2's complement results.
Positive Numbers
Positive 2's complement numbers are represented as the simple binary.
Negative Numbers
Negative 2's complement numbers are represented as the binary number that when added to a positive number of the same magnitude equals zero.

The 2’s complement of a binary number can be obtained by adding 1 to the LSB of the 1’scomplement:
2’s complement = (1’s complement) + 1

 The most significant (leftmost) bit indicates the sign of the integer;    therefore it is sometimes called the sign bit.
If the sign bit is zero,
then the number is greater than or equal to zero, or positive.
If the sign bit is one,
then the number is less than zero, or negative.

Calculation of 2's Complement
To calculate the 2's complement of an integer, invert the binary equivalent of the number by changing all of the ones to zeroes and all of the zeroes to ones (also called 1's complement), and then add one.
For example,          
0001 0001(binary 17)   such that   1110 1111(two's complement -17)

NOT(0001 0001)
 = 
1110 1110  (Invert bits)
1110 1110 + 0000 0001
 = 
1110 1111  (Add 1)

FOR EXAMPLE:
Express the decimal number -55 as an 8-bit in the sign-magnitude, 1’scomplement, and 2’s complement forms.

SOLUTION:
          8-bit number for + 5510 = 001101112

          Sign-magnitude form for -5510 = 101101112

Change the sign bit to a 1 and leave the magnitude bits as they are 1’s complement form for -5510 = 110010002

Take the 1’s complement of +55 by changing all 1’s to 0s and 0s to 1s
           
          2’s complement form for -5510 =
11001000              1 's complement
                                          +                      1
                                                11001001              2 's complement

 Two's Complement Addition
1. 47 + 23
    Since 47 and 23 are not negative integers, we don't have to use two's complement for addition.
    47 = (32 + 8 + 4 + 2 + 1) = 101111
    23 = (16 + 4 + 2 + 1) = 10111
     1 1 1 1 1    carry row
     1 0 1 1 1 1
+      1 0 1 1 1
  1 0 0 0 1 1 0
     1000110 = 70
Notice that no sign bit is added in this instance, nor is the leftmost bit to be interpreted as a negative.

2. 72 + (-100)
    72 = 01001000
    100 = 01100100, and the two's complement of 01100100 = 10011100.
             1 1       carry row
     0 1 0 0 1 0 0 0
   + 1 0 0 1 1 1 0 0
     1 1 1 0 0 1 0 0
There is no overflow, and the leftmost digit is a 1, indicating that the result is a negative number.  First use 2's complement to convert 11100100 to 00011011, then convert 00011011 to decimal representation and take the negative. The answer is -28.

4. (-35) + (-58)
     -35 = -00100011 = 11011101 (two's complement of 35)
     -58 = -00111010 = 11000110 (two's complement of 58)
    1   1 1 1       carry row
    1 1 0 1 1 1 0 1
 +  1 1 0 0 0 1 1 0
  1 1 0 1 0 0 0 1 1
     After truncating the overflow 1, we get the negative binary number 10100011. Converting 10100011 to decimal representation by two's complement gives us a result of -93.  This last problem illustrates that, if you had decided to use only 7 bits to represent your numbers, you would have come up with an incorrect, positive number.  For now, when adding 2 negative number , make sure that the "carries" don't carry the negative sign bit out - compensate by adding an extra bit when making calculations

Two's complement subtraction is the binary addition of the minuend to the 2's complement of the subtrahend (adding a negative number is the same as subtracting a positive one).
For example,
7 - 12 = (-5)

0000 0111
=
+7
+ 1111 0100


=
-12
1111 1011
=
-5

Two's complement multiplication follows the same rules as binary multiplication.
For example,
(-4) × 4 = (-16)

1111 1100
=
-4
× 0000 0100


=
+4
1111 0000
=
-16

Two's complement division is repeated 2's complement subtraction. The 2's complement of the divisor is calculated, then added to the dividend. For the next subtraction cycle, the quotient replaces the dividend. This repeats until the quotient is too small for subtraction or is zero, then it becomes the remainder. The final answer is the total of subtraction cycles plus the remainder.
For example,
7 ÷ 3 = 2 remainder1

00000111
=
+7

00000100
=
+4
11111101


=
-3
11111101


=
-3
00000100
=
+4
0000 0001
=
+1 (remainder)



written by TEH WEI HAN

No comments:

Post a Comment