In a binary number, the bit furthest to the left is called the most
significant bit (msb) and the bit furthest to the right is called the
least significant bit (lsb).
msb
|
10010010
|
lsb
Four systems for representing negative numbers:
Signed Magnitude
The MSB gives the sign of the number (sign bit) , 0 for positive and
1 for negative. The remaining bits hold the magnitude of the number.
e.g.: 4=00000100, -4=10000100
One's Complement
The MSB is also the sign but to negate a number all the bits are complemented
(1 is replaced by 0 and 0 is replaced by 1)
e.g.: 4=00000100, -4=11111011
Signed Magnitude and One's Complement are not used often because they
have two representations for 0 (+0 and -0).
Excess n Notation
Add n to the number. n is usually 2m-1 where m is the precision.
For an 8 bit number, n is 128.
e.g.:4=10000100, -4=011111100 (-4+128=124)
Two's Complement
This is the same as ones complement but negative numbers have one added
to them. This is so that there are not two zeros (+0 and -0). The MSB is
still the sign.
e.g.:4=00000100, -4=11111100.
Two's Complement is the most common representation for negative numbers.
Because it has the following properties:
Positive numbers are the same as the unsigned representation.
Unsigned addition and subtraction works for signed numbers.
There is only one representation for zero.
It is used by the machine running the following program.
The diagram below shows the relationship between binary, signed and
unsigned numbers for 8 bit two's complement.
Moving clockwise around this diagram corresponds to addition; moving
anti-clockwise corresponds to subtraction.
Note that there are two places on the diagram where an addition or subtraction
will cause a result that is not valid.
For unsigned numbers, crossing the top of the diagram.
10-11=255.....Wrong.
250+9=3..........Wrong again.
These cases cause a Carry.
For signed numbers, crossing the bottom of the diagram.
120+10=-126.....Wrong.
-100-29=127.....Wrong again.
These cases cause an Overflow.
Similar diagrams can be drawn for the other representations of binary
numbers to illustrate their disadvantages.
In C, there are 4 standard types of integer:
int
Signed Integer using two's complement with 16,32 or 64 bits of precision
(implementation dependent, 32 is most common).
unsigned
Unsigned integer with 16,32 or 64 bits or precision.
long
Signed Integer using two's complement with 32 or 64 bits of precision.
unsigned long
Unsigned Integer using two's complement with 32 or 64 bits of precision.
char
Signed Integer using two's complement with 8 bits of precision.
unsigned char
Unsigned Integer with 8 bits of precision.
Addition and Subtraction of binary numbers
Two binary numbers A and B are added from right to left, creating a sum
and a carry in each bit position.
Since the rightmost bits of A and B can each assume one of two values,
four cases must be considered: 0 + 0, 0 + 1, 1 + 0, and 1 + 1.
For the remaining bit positions, the carry into the position can be
0 or 1, so that a total of eight input combinations must be considered.
Subtraction is performed in a similar manner using different rules.
For subtraction the carry is often called a borrow.
These operations are performed by the CPU (you rarely have to perform
operations on individual bits).
The order of numbers is preserved when representing signed integers
using twos complement.
This means that the same addition and subtraction can be used for signed
and unsigned numbers.
The only difference is that a signed arithmetic can produce overflows
and unsigned arithmetic can produce carrys.