1.4.1 - Signed binary and integer arithmetic
An eight-bit pattern can mean a positive unsigned integer or a negative signed integer, depending on its interpretation. This part of 1.4.1 compares sign and magnitude with two's complement, then uses fixed-width binary addition and subtraction. You will distinguish a discarded carry from signed overflow and check whether a computed answer can actually fit.
Sign and magnitude separates sign from size
Negative numbers need an agreed way to store both their sign and their size.
Sign and magnitude
In sign and magnitude, the leftmost bit stores the sign: 0 means non-negative and 1 means negative. The remaining bits store the magnitude using ordinary positive binary weights.
For eight bits, seven magnitude bits give a maximum magnitude of 127, so the range is -127 to +127.
To store -37 in eight-bit sign and magnitude, write positive magnitude 37 as seven bits: 0100101, because . Prefix sign 1 to obtain 10100101. Decode 11001010 by separating sign 1 from magnitude 1001010; the magnitude is , so the value is -74.
There are two zero patterns: 00000000 and 10000000. They both have zero magnitude, with different signs. Changing the sign bit changes the sign of the value but leaves the magnitude unchanged. Ordinary binary addition of the complete patterns does not in general implement signed arithmetic: adding sign bits as ordinary weights would treat them as 128 rather than as signs.
Two's complement gives the leftmost bit a negative weight
Two's complement gives the complete pattern a signed numeric value.
Two's-complement integer
In an -bit two's-complement integer, the leftmost bit has weight ; the remaining positions have positive binary weights. The leftmost bit is part of the weighted value, not a separate minus symbol.
For eight-bit integers the weights are -128, 64, 32, 16, 8, 4, 2 and 1. The range is -128 to +127. A leading 1 therefore signals a negative value, but the remaining bits are not its positive magnitude.
Decode 11011011 directly: . To encode -37, first write +37 as 00100101, invert every bit to 11011010, then add one to obtain 11011011. This works at the specified width because the resulting pattern is the complement to : , whose unsigned pattern is 11011011. Its signed interpretation is .
The resulting -bit range is through . Zero has one representation, all zeros. The most negative value has no positive partner at that width: negating eight-bit 10000000 by inversion plus one returns the same pattern because +128 is outside the range.
When increasing a signed two's-complement width, copy the sign bit into the new leading positions: eight-bit 11011011 becomes twelve-bit 111111011011, still -37. Adding zeros on the left would instead change it to positive 219. This sign extension preserves the value; it is useful when making room for intermediate arithmetic.
Add with carries at a fixed width
Binary addition works column by column from the right. 0 + 0 gives 0, 0 + 1 gives 1, and 1 + 1 gives binary 10: write 0 and carry 1 left. Including an incoming carry, 1 + 1 + 1 gives 11: write 1 and carry 1.
For a small unsigned example, 13 plus 11 is:
00001101
+ 00001011
= 00011000
From bit 0 to bit 4, the column totals are 2, 2, 2, 3 and 1 after including each carry. They write 0, 0, 0, 1 and 1, giving 24. This exposes the binary mechanism rather than only checking 13 + 11 in denary.
The same addition mechanism works on two's-complement patterns. For -37 plus +12:
11011011 (-37)
+ 00001100 (+12)
= 11100111 (-25)
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account