4.5.4.1–4.5.4.2 - Unsigned binary arithmetic
In 4.5.4.1 you will interpret unsigned values and their limits. In 4.5.4.2 you will add and multiply binary integers, checking whether the result fits the available bits.
Unsigned values and ranges
An unsigned integer uses every bit as a non-negative power-of-two contribution.
Unsigned binary represents non-negative integers. Signed binary uses a coding scheme that also represents negative integers, so the same bits may mean a different value. For example, is 15 as a four-bit unsigned integer; it does not have the same meaning in the signed two's-complement scheme taught next.
Convert unsigned binary by adding its positive place values: . To encode 22 in eight bits, pad with leading zeros: . The number of available bits is part of the representation, even though padding does not change an unsigned value.
For unsigned bits, the range is to , inclusive. There are different values.
The minimum is the all-zero pattern. The maximum is the all-one pattern because every positive place value contributes. Five bits have values 0–31, not 0–32: counting both endpoints gives 32 values. Eight bits have values 0–255. A desired value of 256 needs a ninth bit because it is .
Binary addition and carries
Binary addition follows place-value addition, but a total of two in one column becomes zero with a carry to the next column.
The column rules are , , and . With an incoming carry, : write 1 and carry 1. Align the rightmost bits and work from right to left.
Consider . The rightmost column is . The next is , so write 0 and carry 1. The next is , so write 0 and carry 1. The leftmost original column is , so write 0 and carry a new leading 1. The full sum is . The decimal check is .
If only four result bits are available, 17 exceeds the maximum of 15. This is unsigned overflow: the correct mathematical result needs more bits than the field provides. Retaining only the low four bits would give , or 1, which is not the sum. State the required width and whether overflow occurs; a carry beyond the available most-significant bit detects unsigned addition overflow.
A leading carry does not automatically mean an incorrect calculation: it can be the correct extra bit of a wider answer. For example, a five-bit destination can store the full result 10001 without overflow.
Binary multiplication and result width
Multiplication builds a sum of shifted copies of the multiplicand, one for each 1 bit in the multiplier.
Multiplying by 0 produces zero; multiplying by 1 preserves the value. Multiplying a non-negative integer by , or two, moves its digits one position left and appends zero. This is a place-value multiplication only when the result width is sufficient: dropping the leading bit can change the stored answer.
For , the multiplier contains a 1 in its units position and a 1 in its twos position. The partial products are and . Adding them gives , matching . A zero multiplier bit contributes a zero row, not a shifted copy with value added.
For , the partial products are for the units bit, zero for the twos bit, and for the fours bit. Their sum is , matching . The full product fits five unsigned bits, since the maximum is 31, but not four bits, whose maximum is 15.
Multiplying an -bit unsigned value by a -bit unsigned value may require up to bits for the full product. The largest product is . A particular smaller product may need fewer bits. If the destination is fixed, compare the actual full product with its maximum rather than assuming the operand width will be sufficient.
Keep full binary carries and partial products while calculating. Decide whether the final result fits only after interpreting the destination width.