140 likes | 268 Views
Lecture. Signed Integer Representation. Sign and Magnitude One’s Complement Two’s Complement. Sign and Magnitude. Requires one bit to represent sign 0 for positive 1 for negative In 8 bit allocation you can only use 7 bits to represent absolute value of a number
E N D
Signed Integer Representation • Sign and Magnitude • One’s Complement • Two’s Complement
Sign and Magnitude • Requires one bit to represent sign • 0 for positive • 1 for negative • In 8 bit allocation you can only use 7 bits to represent absolute value of a number • Range: - (2N -1) to + (2N -1) = -127 to +127
Example • Store -258 in a 16 bit memory location using sign-and-magnitude representation • Solution: • First change the number to binary • 100000010 • Add 6 zeros to make a total of N-1 bits • 000000100000010 • Add an extra one on the left to show that the number is negative • 1000000100000010
Representation of Zero in Sign and Magnitude Representation • Issue: Two representations of zero • +0 00000000 • -0 10000000
One’s Complement • If the sign is positive no action is needed • If the sign is negative: • every bit is complemented • (changed from 0 to 1 or from 1 to 0) • The left most bit defines the sign of the number
Example • Store -258 in a 16 bit memory location using one’s complement representation • Change the number to Binary • 100000010 • Add 7 zeros to the left so that there is a total of N(16) bits • 0000000100000010 • The sign is negative, so each bit is complemented • 1111111011111101
Interpretation of One’s Complement • 1111111011111101 • If the left most bit is 0: • Change the entire number from binary to decimal • If the left most bit is 1 (Negative Number) • Complement all bits • 0000000100000010 • Convert to decimal • 258 • Add (-ve) sign in front of the number • (-258)
Representation of Zero in One’s Complement • There are two representations of 0’s: • +0 00000000 • -0 11111111
Two’s Complement • Twos’ complement is the most common, the most important and the most widely used representation of integers today • If sign is positive do nothing. • If the sign is negative: • Leave all the right most zeros and first 1 unchanged. • Complement the rest of the bits.
Example • Store -40 in a 16 bit memory location using two’s complement representation • Change the number to binary: • 101000 • Add 10 zeros so that there are a total of N(16) bits • 0000000000101000 • The sign is negative so leave the right most zeros up to the first 1 (including the 1) unchanged and complement the rest: • 1111111111011000
Interpretation of Two’s Complement • Interpretation of two’s complement • 1111111111011000 • If the left most bit is 0, change the whole number from binary to decimal and put a + sign • If the left most bit is 1 • Leave the right most bits up to the first 1 (inclusive) unchanged and complement the rest • 0000000000101000 • Change the whole number from binary to decimal • 40 • Put a – sign in front of the number • (-40)