Representations of signed numbers In binary systems, an n-bit string can represent 2n different numbers 01234567 4-3210123 000001010011100101110111????000001010011 3-bit string unsigned numbers 3-bit string signed numbers Unsigned: 0--2n-1 Signed:-(2n--1-2n--1
Representations of signed numbers In binary systems , an n-bit string can represent 2n different numbers ! Unsigned: 0—2 n -1 signed: -(2n-1 -1)—2 n-1 -1
Representations of signed numbers If we want to represent 2n different numbers and their negative part, we have to use n+i bits i 0001001101010111 8了765 3210 234567 ????????0000001001000110 4-bit string MSB is not a value bit, but a sign bit Negative: 1 Positive: 0
Representations of signed numbers If we want to represent 2n different numbers and their negative part, we have to use n+1 bits ! MSB is not a value bit , but a sign bit : Negative : 1 Positive : 0
How to represent negative signed numbers Signed-magnitude code(s-M code): reversal 6 5 2 34 1111110110111001011101010010000001001000110100010101100111 1000 Ones complimentary code shift 65 32 3456 100010011010101111001101111000000001001000110100010101100111 1111 Twos complimentary code shift! 65432-101 1000100110101011110011011110111100000001001000110100010101100111
How to represent negative signed numbers Signed-magnitude code(S-M code): reversal ! One’s complimentary code : shift ! Two’s complimentary code : shift !
Converting rules for signed numbers From unsigned to signed Its msb is changed to the sign bit The number must be a positive Beware the overflow i Key point a positive signed num ber with any code must be the same! Its msb is“0”!
Converting rules for signed numbers From unsigned to signed: Its MSB is changed to the sign bit. The number must be a positive ! Beware the overflow ! Key point: A positive signed number with any code must be the same ! Its MSB is “0” !
Change the sign of a signed number signed-magnitude change the sign-bit; +21:00010101-21:10010101 one's complement: change every bit; +21:00010101-21:11101010 two’ s complement: change every bit, and add l at lsB +21:00010101-21:11101010 note: keep the width of the string in addition
Change the sign of a signed number signed-magnitude : change the sign-bit; +21: 0001 0101 -21: 1001 0101 one’s complement: change every bit; +21: 0001 0101 -21: 1110 1010 two’s complement: change every bit, and add 1 at LSB; +21: 0001 0101 -21: 1110 1010 note: keep the width of the string in addition !
Conversion from one code to another Step 1: check the Msb(sign-bit); For positive number (msb=0): no changes Step 2: For negative number (MSB=1) first change its sign by the former code way then change its sign by the next code way
Conversion: from one code to another Step 1: check the MSB (sign-bit) ; For positive number (MSB =0) : no changes! Step 2: For negative number (MSB =1) : first change its sign by the former code way; then change its sign by the next code way !
Conversion: from one code to another Examples s-M =01101001 B =10101001 Cnms=11001010 B twos s-M C,=? one s
Conversion: from one code to another Examples: AS−M = 01101001 Bone's =10101001 Ctwo's =11001010 ? Atwo's = = ? BS−M ? Cone's =
Addition for signed numbers 01015 01015 01015 +1011-3 1100-3 +1101-3 00000 00011 00102 S-M code Ones code Two's code Use addition rules for unsigned numbers only 'Twos code can get right result
Addition for signed numbers Use addition rules for unsigned numbers , only Two’s code can get right result !
Rules for Twos complement addition I two numbers must be the same length keep the length in addition. 2 subtract a number means add the negative number i Examples: 1110+1010=1000 0110-0011=0110+1101=0011
Rules for Two’s complement addition 1 two numbers must be the same length, keep the length in addition. 2 subtract a number means add the negative number ; Examples: 1110 + 1010 = 1000 0110 – 0011 = 0110 + 1101 = 0011
Twos complement addition Overflow the result exceeds the number ranges condition two numbers has the same sign; checking: the sign is wrong solving increase the data bit-length Examples 0101+0101=1011 101+1010=0111
Two’s complement addition Overflow : the result exceeds the number ranges ; condition : two numbers has the same sign; checking: the sign is wrong! solving : increase the data bit-length ! Examples: 0101 + 0101 = 1011 1101 + 1010 = 0111