Monday, April 15, 2013

two's complement

  • two's complement is the way every computer I know of chooses to represent integers. To get the two's complement negative notation of an integer, you write out the number in binary. You then invert the digits, and add one to the result.


Suppose we're working with 8 bit quantities (for simplicity's sake) and suppose we want to find how -28 would be expressed in two's complement notation. First we write out 28 in binary form.

00011100
Then we invert the digits. 0 becomes 1, 1 becomes 0.

11100011
Then we add 1.

11100100
That is how one would write -28 in 8 bit binary.



Example 2

Now suppose we want to subtract 12 from 69. Now, 69 - 12 = 69 + (-12). To get the negative of 12 we take its binary representation, invert, and add one.

0000 0000 0000 0000 0000 0000 0000 1100
Invert the digits.

1111 1111 1111 1111 1111 1111 1111 0011
And add one.

1111 1111 1111 1111 1111 1111 1111 0100
The last is the binary representation for -12. As before, we'll add the two numbers together.

1111 1111 1111 1111 1111 1111 1    1
Carry Row
  0000 0000 0000 0000 0000 0000 0100 0101
(69)
+ 1111 1111 1111 1111 1111 1111 1111 0100
(-12)
  0000 0000 0000 0000 0000 0000 0011 1001
(57)

We result in 57, which is 69-12.

Example 3

Lastly, we'll subtract 69 from 12. Similar to our operation in example 2, 12 - 69 = 12 + (- 69). The two's complement representation of 69 is the following. I assume you've had enough illustrations of inverting and adding one.

1111 1111 1111 1111 1111 1111 1011 1011
So we add this number to 12.
111  
Carry Row
  0000 0000 0000 0000 0000 0000 0000 1100
(12)
+ 1111 1111 1111 1111 1111 1111 1011 1011
(-69)
  1111 1111 1111 1111 1111 1111 1100 0111
(-57)

This results in 12 - 69 = -57, which is correct.

http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html





  • Arithmetic operations


Addition
Adding two's-complement numbers requires no special processing if the operands have opposite signs: the sign of the result is determined automatically. For example, adding 15 and −5:
 11111 111   (carry)
  0000 1111  (15)
+ 1111 1011  (−5)
==================
  0000 1010  (10)
This process depends upon restricting to 8 bits of precision; a carry to the (nonexistent) 9th most significant bit is ignored, resulting in the arithmetically correct result of 1010.

http://en.wikipedia.org/wiki/Two's_complement






  • 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)      1110 1111(two's complement -17)

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


2's Complement Addition

Two's complement addition follows the same rules as binary addition.

For example,

5 + (-3)  =  2    0000 0101 = +5
+ 1111 1101
 = -3
  0000 0010 = +2



 2's Complement Subtraction

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



2's Complement Multiplication
Two's complement multiplication follows the same rules as binary multiplication.


2's Complement Division
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
http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm#calculate



  • two's complement notation
If you have -30, and want to represent it in 2's complement,

you take the binary representation of 30
0000 0000 0000 0000 0000 0000 0001 1110

Invert the digits.
1111 1111 1111 1111 1111 1111 1110 0001

And add one.
1111 1111 1111 1111 1111 1111 1110 0010


Suppose we're working with 8 bit quantities
suppose we want to find how -28 would be expressed in two's complement notation

First we write out 28 in binary form.
00011100

Then we invert the digits. 0 becomes 1, 1 becomes 0.
11100011

Then we add 1.
11100100

That is how one would write -28 in 8 bit binary.


Two's Complement
Begin with the number in one's complement.
Add 1 if the number is negative

12 would be represented as 00001100 and -12 as 11110100.

To verify this, let's subtract 1 from 11110100, to get 11110011. If we flip the bits, we get 00001100, or 12 in decimal.

http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary#sign

No comments:

Post a Comment