Two`s Complement Representation (for Signed Integers)

advertisement
Two’s Complement Representation (for Signed Integers)
To store a POSITIVE integer, just place a zero in the leftmost bit. A positive signed
integer looks just like an unsigned integer, except that the most significant bit can
NEVER be 1.
To store a NEGATIVE integer, you need to do some conversion, as explained in the
next section. The resulting value will always contain a 1 in the most significant bit.
Negative Decimal to Two’s Complement Conversion
There are two different methods you can use to convert a negative decimal integer to
two’s complement form.
Method #1
There are three steps necessary to convert a negative decimal integer to two’s
complement form using method #1.
1) Always START with the positive binary value, expanded to fill the number of bits you
will be storing the number into.
Example: To store –22 in 1 byte, start with the binary value for +22
So start with:
00010110
NOTE: The value CANNOT extend into the leftmost bit – the leftmost bit MUST
be zero.
2) Complement/flip all of the bits. This means all of the 1s become 0s and all of the 0s
become 1s.
Example (continued):
Flip all bits
00010110  11101001
3) Add one to the flipped bits.
Example (continued):
11101001
+1
11101010
So the decimal value –22 would be stored into 1 byte of memory as:
1110 1010
Another Example using Method #1
Convert –76 to 8-bit two’s complement:
1)
7610 converted to binary is 010011002
2)
Complement
3)
Add 1:
So:
-7610 = 10110100 and would be stored into 1 byte of memory as:
01001100  10110011
10110011
+ 1
10110100
1011 0100
Method #2
Another method (some call it the “magic” method) for converting negative decimal
integers to two’s complement values only requires 2 steps.
1) Again START with the positive binary value, expanded to fill the number of bits you
will be storing the number into.
Example: To store –22 in 1 byte, start with the binary value for +22
So start with:
00010110
2) Scan the binary representation from right to left. Find the first one bit and
complement the remaining pattern to the left.
Example (continued):
00010110
First one bit from right is:
00010110
Flip all bits to the left of the one bit:
11101010
So the decimal value –22 would be stored into 1 byte of memory as:
1110 1010
As you can see, this is the same answer that we got using the previous method to store
–22.
Another Example using Method #2
Convert –76 to 8-bit two’s complement:
1)
7610 converted to binary is:
010011002
2)
First one bit from right is:
01001100
Flip all bits to the left of the one bit:
10110100
So the decimal value –76 would be stored into 1 byte of memory as:
1011 0100
Again you can see, this is the same answer that we got using the previous method to
store –22.
Two’s Complement to Decimal Conversion
The conversion process is different for positive vs. negative values. First examine the
sign bit.
Positive Values: If the (leftmost) sign bit is zero, the value is positive. Simply do a
binary to decimal conversion.
Example:
00100111 (in two’s complement form)
= 32 + 4 + 2 + 1
= +39
So two’s complement 00100111 has a value of +39
Negative Values: If the (leftmost) sign bit is one, the value is negative. You must
convert back from two’s complement form.
Method #1: Follow the three-step conversion process from the last section, in
REVERSE order.
Example:
11100111 (in two’s complement form)
1) Subtract one from the representation:
(i.e. reverse the add)
11100111
- 1
11100110
2) Flip all the bits: 11100110  00011001
3) Do a binary to decimal conversion and then add a negative sign.
00011001
=
=
16 + 8 + 1
-25
So two’s complement 00011001 has a value of -25
Method #2: Again find the first one bit, starting from the rightmost bit, and flip all bits to
the left of it. Then convert to decimal.
Example:
1)
11100111 (in two’s complement form)
First one bit from right is:
11100111
Flip all bits to the left of the one bit:
00011001
2) Do a binary to decimal conversion and then add a negative sign.
00011001
=
=
16 + 8 + 1
-25
Download