Negative Integers
- There are numerous ways to represent both positive and
negative integers on a computer. Here are a couple of the more popular:
- In the simple sign bit, one of the bits is declared to be
the sign bit and the number is interpreted to be positive when the bit is 0
and negative when the bit is 1.
- In a biased representation, the integer i is stored
internally as i + MAX_INT/2, where MAX_INT is the largest storeable
integer. Thus in a biased representation, integers can range from
-MAX_INT/2 to MAX_INT/2.
- Two's complement is the most popular representation
technique for storing both positive and negative integers. It is based
on the observation that if I am storing my integers in eight bits, then
the pattern 11111111 (eight ones in a row) behaves a lot like -1.
In particular, if I add 11111111 and 1, then I get 100000000 which becomes
00000000 when I store it back into 8 bits. In general, to find -x
all I do is flip all of the bits in x (this is called the
one's complement of x and can be performed in C with
the bit-wise complement operator ~) and then add 1. The great
advantage of two's complement notation is that the same routines that
add non-negative integers can be used to add signed integers.
Next Slide