Computer Science 210 Computer Organization Number Systems Signed Integers
Negatives: Sign-Magnitude With a fixed number of bits, say N The leftmost bit is used to give the sign 0 for positive number 1 for negative number The other N-1 bits are for the magnitude Example: -25 with 8 bit numbers Sign: 1 since negative Magnitude: 11001 for 25 8-bit result: 10011001 Note: This would be 153 as a positive.
Ranges for N-bit numbers Unsigned (positive) 0000…00 or 0 1111…11 which is 2N-1 For N=8, 0 - 255 Sign-magnitude 1111…11 which is -(2N-1-1) 0111…11 which is 2N-1-1 For N=8, -127 to 127 2’s Complement 1000…00 which is -2N-1 0111…11 which is 2N-1 - 1 For N=8, -128 to 127
Sign-Magnitude: Pros and Cons Easy to comprehend Easy to convert Con: Addition complicated (expensive) If signs same then … else if positive part larger … Two representations of 0
Negatives: Two’s complement With N bit numbers, to compute negative Invert all the bits Add 1 Example: -25 in 8-bit two’s complement 25 00011001 Invert bits: 11100110 Add 1: 1 11100111
2’s Complement: Pros and Cons Not so easy to comprehend Human must convert negative to identify Pro: Addition is exactly same as for positives No additional hardware for negatives, and subtraction. One representation of 0
2’s Complement: Examples Compute negative of -25 (8-bits) We found -25 to be 11100111 Invert bits: 00011000 Add 1: 00011001 Recognize this as 25 in binary Add -25 and 37 (8-bits) 11100111 (-25) + 00100101 ( 37) (1)00001100 Recognize as 12
Facts about 2’s Complement Leftmost bit still tells whether number is positive or negative as with sign-magnitude 2’s complement is same as sign magnitude for positives
2’s complement to decimal (examples) Assume 8-bit 2’s complement: X = 11011001 -X = 00100110 + 1 = 00100111 = 32+4+2+1 = 39 (decimal) So, X = -39 X = 01011001 Since X is positive, we have X = 64+16+8+1 = 89