Representations of signed numbers
In binary systems,an n-bit string can
represent 2n different numbers !
Unsigned,0—2n-1 signed,-(2n-1-1)—2n-1-1
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 !
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 !
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
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
Examples,
0 1 1 0 1 0 0 1 MSA
1 0 1 0 1 0 0 1'?so n eB
11001010'?st w oC
'?stw oA MSB?'?soneC
Addition for signed numbers
Use addition rules for unsigned numbers,
only Two’s code can get right result !
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
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
Increase the length of the signed number
S-M,add 0 bit after the signed-bit;
Others,add signed-bit before MSB;
Examples:
S-M,0101 => 0000 0101
1100 => 1000 0100
OTHERS,0101 => 0000 0101
1100 => 1111 1100
Multiplication for signed numbers
Only S-M’s code can get right result !
The sign-bits 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!
In binary systems,an n-bit string can
represent 2n different numbers !
Unsigned,0—2n-1 signed,-(2n-1-1)—2n-1-1
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 !
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 !
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
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
Examples,
0 1 1 0 1 0 0 1 MSA
1 0 1 0 1 0 0 1'?so n eB
11001010'?st w oC
'?stw oA MSB?'?soneC
Addition for signed numbers
Use addition rules for unsigned numbers,
only Two’s code can get right result !
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
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
Increase the length of the signed number
S-M,add 0 bit after the signed-bit;
Others,add signed-bit before MSB;
Examples:
S-M,0101 => 0000 0101
1100 => 1000 0100
OTHERS,0101 => 0000 0101
1100 => 1111 1100
Multiplication for signed numbers
Only S-M’s code can get right result !
The sign-bits 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!