Thursday, 3 February 2011

2's Complement Representation for Signed Integers





Question 2
2's Complement Representation for Signed Integers


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.

Integer2's Complement
SignedUnsigned
550000 0101
440000 0100
330000 0011
220000 0010
110000 0001
000000 0000
-12551111 1111
-22541111 1110
-32531111 1101
-42521111 1100
-52511111 1011

Note:  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)



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.
For example,

(-4) × 4  =  (-16)     1111 1100 = -4
× 0000 0100
 = +4
  1111 0000 = -16



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.
For example,

7 ÷ 3  =  2 remainder 1     0000 0111 = +7     0000 0100 = +4
+ 1111 1101
 = -3+ 1111 1101
 = -3
  0000 0100 = +4  0000 0001 = +1 (remainder)



Sign Extension

To extend a signed integer from 8 bits to 16 bits or from 16 bits to 32 bits, append additional bits on the left side of the number. Fill each extra bit with the value of the smaller number's most significant bit (the sign bit).
For example,

Signed Integer8-bit Representation16-bit Representation
-11111 11111111 1111 1111 1111
+10000 00010000 0000 0000 0001



Other Representations of Signed Integers

Sign-Magnitude Representation
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).
 
Offset Binary Representation
A third method for representing signed numbers is offset binary. Begin calculating a offset binary code by assigning half of the largest possible number as the zero value. A positive integer is the absolute value added to the zero number and a negative integer is subtracted. Offset binary is popular in A/D and D/A conversions, but it is still awkward for arithmetic computation.
 
For example,

Largest value for 8-bit integer  =  28  =  256
Offset binary zero value  =  256  ÷  2  =  128(decimal)  =  1000 0000(binary)

1000 0000(offset binary 0) + 0001 0110(binary 22) = 1001 0110(offset binary +22)
1000 0000(offset binary 0) - 0000 0111(binary 7) = 0111 1001(offset binary -7)

Signed IntegerSign MagnitudeOffset Binary
+50000 01011000 0101
+40000 01001000 0100
+30000 00111000 0011
+20000 00101000 0010
+10000 00011000 0001
 00000 0000
1000 0000
1000 0000
-11000 00010111 1111
-21000 00100111 1110
-31000 00110111 1101
-41000 01000111 1100
-51000 01010111 1011



Notes

Other Complements
1's Complement  =  NOT(n)  =  1111 1111 - n
9's Complement  =  9999 9999 - n
10's Complement  =  (9999 9999 - n) + 1
 
Binary Arithmetic
Addition
Subtraction
Multiplication
Division
Computer programs process and store numeric data.
A computer game stores the following data:
level of difficulty as an integer in the range 1 to 15
player rating as an integer in the range -120 to +120
fuel level as a number with a fractional part.
This number is in the range 0 to 100
0 8
The level of difficulty is stored as an unsigned binary number using a single byte.
For a particular game, the level of difficulty was set at 11.
Calculate its binary value.
1011
Use the grid for rough working, then copy the bit pattern to your
Electronic Answer Document.
(2 marks)
0 9
A player rating value is stored as a two’s complement integer using a single byte.
Convert the player rating value of 119 into binary
Converting to binary ...
119/128 = 0
119/64 = 1
55/32 = 1
23/16 = 1
7/8 = 0
7/4 = 1
3/2 = 1
1/1 = 1
Final Answer=01110111



-13 2's compliment      11110011

No comments:

Post a Comment