1、Chapter 2 Number systems and codes u Decimal and Binary u Unsigned and Signed u Code for Numbers and CharactersRepresentations of signed numbers In binary systems,an n-bit string can only represent 2n different numbers!Unsigned:02n-1signed:-(2n-1-1)2n-1-1To represent 2n different numbers and their n
2、egative part,we have to use n+1 bits!MSB is not a value bit,but a sign bit:Negative:1 Positive:0 Representations of signed numbersSigned-magnitude code(S-M code):reversal!Ones complement code:shift!Twos complement code:shift!Representations of signed numbersConverting rules for signed numbersFrom un
3、signed to signed:Its MSB is changed to 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:0001 0101 -21:1001 0101 ones complement:change
4、 every bit;+21:0001 0101 -21:1110 1010twos complement:change every bit,and add 1 at LSB;+21:0001 0101 -21:1110 1011From 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
5、its sign by the next code way!Examples:01101001MSA10101001soneB11001010stwoC?stwoA?MSB?soneCFrom one code to another Addition for signed numbersUse addition rules for unsigned numbers,only Twos code can get right result!1 two numbers must be the same width,and keep the width in addition!2 subtract a
6、 number means add the negative number;Examples:1110+1010=1000 0110 0011=0110+1101=0011 Addition for signed numbersOverflow:the result exceeds the number ranges;condition:two numbers has the same sign;checking:the sign is wrong!solving:increase the data width!Examples:0101+0101=1010 1101+1010=0111Add
7、ition for signed numbersS-M:add 0 bit after the signed-bit(MSB);Others:add signed-bit before MSB;Examples:S-M:0101=0000 0101 1100=1000 0100 OTHERS:0101=0000 0101 1100=1111 1100How to increase the number widthMultiplication for signed numbersOnly S-Ms code can be easy done!The sign-bit of the result is the XOR of the sign-bits of the inputs!The value-bits of the result is decided by the multiplication rules for the unsigned numbers!