sections in this module | City
College of San Francisco - CS270 Computer Architecture Module: Machine Basics |
module list |
Signed Numbers
When interpreting data as decimal integers you must know if the
integers are signed or unsigned. Interpretation as unsigned
decimal proceeds similarly to that of other unsigned bases such as
octal or hexadecimal.
If the data is to be interpreted as signed decimal, each datum must be inspected to determine if it is negative. This requires knowing the size of the datum - normally 8, 16, 32 or 64 bits. The most-significant bit of the datum indicates the sign: 1 indicates, of course, a negative number.
If the datum is signed, it is converted to its positive twos-complement counterpart and a sign prefix added.
Let's look at the following binary data
1001001010011111111111101001
Let's assume this consists of three integers: an unsigned four-bit integer followed by a signed eight-bit integer followed by a signed 16-bit integer. Convert each integer to decimal:
Remember, when interpreting a number as signed, before you know
whether the value is positive or negative you must first know what
the size of the datum is. Once you know the size of the datum you
can look at the sign bit.
Converting shorter to longer size data
To convert a short unsigned bit pattern to a longer unsigned bit
pattern just pad the left with the appropriate number of zeroes.
If the data is signed, simply pad the left with the appropriate
number of bits that replicate the most-significant bit of the
original (the sign-bit). This is called sign-extension. (You could also think of a
negative number as having an infinite number of 1s on the left.
Then sign-extension just
increases the number of these bits that are shown.)
In the example above:
Adding and subtracting twos-complement
Subtracting twos complement is simply negating (taking the twos-complement of) the number being subtracted and adding.
Adding twos complement proceeds as you would expect (bitwise addition), but you must consider the issue of overflow.
First, I will try to explain how the book defines overflow: Remember, a negative number in twos-complement has an infinite number of 1's to the left of the number. In other words, for a twos-complement negative number, bit 31 is 1 and so are bits 32-... (which are not shown).
Overflow occurs in addition when the result of the addition has the sign-bit flip but not the hidden bits. Granted, this is a bit confusing, so let's go through this with 4-bit numbers:
Let's start with the number 3 expressed in four bits as 0011. Remember, this number expressed in 8 bits is 00000011, so there are infinite 0 bits to the left in the hidden area. If we add 2 to this number, the result is 0101. The sign bit (bit 3) of our four-bit number is still 0, and so are the hidden bits. If we add 7 to our four-bit number 0011, we get 1010, however, the hidden bits are still 0. Thus we have a mismatch between the hidden bits and our sign bit, and overflow has occurred. (Indeed, decimal 10 is not representable as a signed four-bit number, whose range is -8 to 7.)
As another example, lets start with the number -6 expressed in four bits as 1010. If we add -2 to it (1110) we get 1000, and the 1's still propagate to the left. Thus, the result has the sign-bit and the hidden bits the same. If we add -1 again, the result (0111) has a sign-bit that does not match the hidden bits.
An easier way to explain overflow is that it occurs when the sign of the result is incorrect. If you add two negative numbers and the result is positive (a zero sign-bit), there was overflow. Likewise, if you add two positive numbers and the result is negative (a 1 sign-bit), there was overflow. You can never have overflow from adding a positive and negative number. Thus
overflow
in addition occurs if the two operands are like-signed and the sign of
the result is different than that of the operands.
Prev | This page was made entirely
with free software on linux: Kompozer, the Mozilla Project and Openoffice.org |
Next |