Signed Binary Encoding
Often it is necessary to be able to work with numbers which may be either negative or positive.
The basic unsigned binary encoding scheme can easily be extended to permit signed values by
means of a system called 2's Complement encoding. In terms of processor circuitry, the inclusion
of 2's Complement requires two additional flags: the Sign flag and the Overflow flag.
The shorthand "hexadecimal" notation is a convenience for humans which reduces the difficulties
and number or errors that would arise if we needed to work with internal computer patterns as
long sequences of 0's and 1's.
Allowing Negative Values - 2's Complement Encoding
- Some Analogies of Digital Modular Systems
- What Time Will It Be?
- Suppose that it is now 2 o'clock and that I've been working for 5 hours. At what
time did I start work?
- If it is now 2 o'clock and I started 5 hours ago, to figure out my starting time I
need to subtract 5 hours from 2 o'clock. We seem to be faced with two
- based on our knowledge of the clock, the answer should be 9 o'clock
- based on our knowledge of basic arithmetic, the answer should be (-3)
- What would it mean to say that the time was (-3) o'clock?
- What would it mean to say that the time was 0 o'clock?
- Is 9 o'clock a reasonable way of talking about (-3) o'clock, or about 3 hours
before 0 o'clock, or about 4 hours before 1 o'clock, or about 5 hours before 2
o'clock (all of which are the same thing)?
- How Far Have I driven?
- Suppose I have a (6-digit) car odometer which is set up to reduce the distance it
shows if I drive backwards. Also imagine that when the car is quite new with an
odometer reading of 000004, I drove it backwards for 6 kilometers and then
forward for 10 kilometers.
- the final odometer reading should be 000008
- if the odometer reads 8 (000008) after driving forward 10 kilometers, then
before driving forward 10 kilometers the odometer must have had a reading that
was the equivalent of -2
- the actual odometer reading after driving backwards 6 kilometers from a initial
reading of 4 (000004) kilometers would have been 999998
- for an odometer, 999998 must be the equivalent to -2
- 2's Complement Encoding for Positive Values
- for zero and all positive values, the 2's Complement encoded form for a number is
identical to the (unsigned) binary encoded version
- 2's Complement Encoding for Negative Values (by Subtraction)
- for negative values, the 2's Complement form can be obtained by subtracting the
absolute value of the number from 0 (and ignoring the Carry flag error indicator)
- in a system using an 8-bit word, a word could could be used to encode any value
between 0 and 255 inclusive using the Unsigned Binary Method
- an attempt to encode 256 using Unsigned Binary encoding in an 8-bit word would result
in the same pattern as encoding 0 (except that the Carry flag would likely be turned on
indicating that the limits of Unsigned Binary had been crossed)
- 256 and 0 can therefore be viewed as equivalent values within the context of an 8-bit
- using the 2's Complement method, -1 would be the same (share the same pattern) as
255 in Unsigned Binary; -2 would share the same pattern as 254 in Unsigned Binary;
- 2's Complement Encoding for Negative Values (Alternative Method)
For an negative number:
- encode the absolute value using Unsigned Binary
- reverse the bit pattern
- add 1
- Sign Determination of a 2's Complement Encoded Value
- For decoding purposes, we need some rule to determine whether a given pattern
represents a positive or a negative value
- In 2's Complement, the rule is that any word whose left-most bit is a 1 is negativel any
word whose left-most bit is a 0 is either 0 or positive
- As an example, consider the two patterns 01111111 and 10000000 in an 8-bit word
system. In Unsigned Binary the difference between these values is simply 1 and there is
no particular significance if a count moves up by one from 01111111 to 10000000.
However, in 2's Complement moving from 01111111 to 10000000 is of major
significance since the count has gone from a large positive value to a negative value
(with a large magnitude). The point at which the left-most bit switches therefore
becomes a limit point for 2's Complement, very much like the point where the bits reset
to all 0's from all 1's is a limit for Unsigned Binary encoding.
2 More Flags: Sign and Overflow
- Whenever the results of an arithmetic operation are such that the result should be a large
positive value but appears to be a negative value because the left-most bit is on, or when
the results should be a negative value but appear to be positive because the left-most bit
is off, an Overflow flag is turned on.
- When performing addition or subtraction with values which are intended to be in 2's
Complement encoded form, the addition or subtraction should be followed by a test of
the Overflow flag to catch errors in the result. (This is similar to the Carry flag testing
that should be performed for Unsigned Binary encoded values).
- As a convenience the left-most bit of a result is duplicated as another flag, the Sign flag.
The Sign flag can be tested following 2's Complement arithmetic to determine if the
result of the operation was negative.
- Carry and Overflow Reconsidered for 2's Complement
- the Carry flag has no significance when dealing with 2's Complement encoded values.
Note that addition and subtraction are identical for Unsigned Binary and for 2's
Complement; it is the programmer's responsibility to test the appropriate flag, either
Carry or Overflow.
- since the pattern for the zero value is the same for both Unsigned Binary and 2's
Complement encoding, the Zero flag is still of use when working with 2's Complement.