Carry and signed overflow in an eight-bit adder
Trace eight-bit additions to distinguish unsigned carry from signed overflow, then exhaustively check the sign-bit detection rule.

Carry and signed overflow answer different questions about the same addition. Carry tells you that an unsigned sum needs another bit. Signed overflow tells you that the mathematical result lies outside the chosen signed range. An eight-bit adder can produce one without the other, so a programmer cannot use them interchangeably when checking arithmetic.
Keep the width fixed throughout this example. Eight unsigned bits represent zero through 255. The same bit patterns, interpreted as two’s complement signed integers, represent negative 128 through positive 127. The hardware’s stored low eight result bits are identical under both interpretations; the meaning of those bits changes.
Add 127 and one
The eight-bit representation of 127 is 01111111. Adding 00000001 produces 10000000. No ninth bit is required, so there is no unsigned carry out. The unsigned answer is 128, which fits.
Signed interpretation gives a different diagnosis. Both inputs are nonnegative, but the result pattern represents negative 128. The mathematical sum, positive 128, is outside the signed eight-bit range. This is signed overflow even though carry out is zero.
Now add 255 and one. The full binary sum is 1 00000000: the low eight bits are zero and the ninth bit is one. Unsigned arithmetic overflows its representable range. Under signed interpretation, the inputs are negative one and positive one, whose sum is zero. There is no signed overflow.
| Stored inputs | Low eight result bits | Carry out | Signed overflow |
|---|---|---|---|
| 0x7F + 0x01 | 0x80 | 0 | 1 |
| 0xFF + 0x01 | 0x00 | 1 | 0 |
| 0x80 + 0xFF | 0x7F | 1 | 1 |
| 0x20 + 0x10 | 0x30 | 0 | 0 |
The third row represents negative 128 plus negative one when read as signed values. The mathematical answer, negative 129, does not fit. A positive result from two negative operands is the visible symptom in this case.
Derive a test from sign bits
For addition, signed overflow occurs when the operands have the same sign and the truncated result has the opposite sign. If the operands have different signs, the sum falls between them and cannot exceed both signed endpoints. This rule is specific to addition; subtraction needs its corresponding sign analysis.
Write the operand patterns as A and B, the masked result as R, and the sign-bit mask as 0x80. The expression ~(A ^ B) has a one at the sign position when the input signs agree. The expression (A ^ R) has a one there when the result sign differs from A. AND those values with the sign mask to detect overflow.
def signed8(bits):
return bits if bits < 128 else bits - 256
for a in range(256):
for b in range(256):
wide = a + b
result = wide & 0xff
carry = wide > 0xff
overflow = bool((~(a ^ b) & (a ^ result)) & 0x80)
exact_signed = signed8(a) + signed8(b)
assert overflow == (not -128 <= exact_signed <= 127)
assert carry == (a + b >= 256)
print("65536 additions passed")
The arbitrary-precision Python sum supplies an independent mathematical reference. Masking creates the narrow hardware result; interpreting the original patterns supplies signed operands. Keeping those steps separate avoids accidentally asking Python’s unbounded integers to behave like an eight-bit register.
A language may promise something different
A processor instruction and a programming-language expression have separate contracts. The RISC-V RV32I integer instruction specification says that integer arithmetic instructions write the low XLEN bits and ignore arithmetic overflow. RISC-V base integer instructions do not provide the same condition-code register found in some other instruction sets.
That rule does not mean an overflowing signed C expression is automatically well-defined. C unsigned arithmetic uses modular rules, while a signed expression whose result is not representable can have undefined behavior. The C11 committee draft N1570, sections 6.2.5 and 6.5 paragraph 5, states these separate rules. The compiler may reason from those semantics before selecting instructions. If your application requires checked, saturating, or wrapping signed arithmetic, express that policy using the language’s supported operations or carefully bounded wider types.
Even a wider type requires a bound. An eight-bit addition fits safely in a sufficiently wide integer, but multiplying two large values and accumulating thousands of products can exceed a seemingly generous accumulator. Decide how many terms exist and what range each term can reach before choosing a representation.
Derive overflow from the final carry stage
The sign-bit rule has an equivalent gate-level form: signed overflow is the XOR of the carry into the most significant bit and the carry out of that bit. For an eight-bit adder, call those carries C7 and C8. Then the overflow indication is C7 XOR C8. Unsigned carry is simply C8.
Consider 127 plus one again. The low seven bits add to 128, sending a carry into the sign position. Both sign input bits are zero, so adding that carry produces a sign bit of one and no carry out. C7 is one, C8 is zero, and their XOR is one. For 255 plus one, the same incoming carry meets a sign input of one; the sign sum becomes zero and the outgoing carry is one. Both carries are one, so signed overflow is zero.
One way to prove this identity is to enumerate the sign-stage inputs: the two operand sign bits and the carry from the lower bits. There are eight cases. For each case, calculate the sign result and carry out, then compare the same-sign/different-result rule with the carry XOR. This proof concerns the final full-adder stage; it does not assume how the lower seven bits produced their carry.
from itertools import product
for sign_a, sign_b, carry_in in product((0, 1), repeat=3):
total = sign_a + sign_b + carry_in
sign_result = total & 1
carry_out = total >> 1
sign_rule = (sign_a == sign_b) and (sign_result != sign_a)
assert sign_rule == bool(carry_in ^ carry_out)
print("8 sign-stage cases passed")
The same reasoning extends to other widths by moving the sign position. It does not imply that a particular instruction set exposes either carry signal to software. Some architectures have condition flags, while RISC-V base integer code uses other operations to implement checks.
Subtraction changes the sign test
A fixed-width subtractor can calculate A minus B using A plus the bitwise complement of B plus one. Keep the complement limited to the chosen width. The resulting carry out commonly means that an unsigned borrow was not needed, but an architecture’s named carry or borrow flag must be interpreted from its own manual.
For eight-bit inputs, 0x00 minus 0x01 produces low bits 0xFF with no carry out from that add-complement operation. Unsigned zero minus one requires a borrow. As signed arithmetic, the exact result is negative one and fits, so signed overflow is false.
Now subtract one from 0x80. The low result is 0x7F, and the unsigned subtraction of 128 minus one needs no borrow. Signed interpretation asks for negative 128 minus one, which is negative 129 and does not fit. For subtraction, signed overflow occurs when the operand signs differ and the result sign differs from A. The corresponding bit expression is ((A ^ B) & (A ^ R) & 0x80) != 0.
def signed_byte(bits):
return bits if bits < 128 else bits - 256
for a in range(256):
for b in range(256):
wide = a + (b ^ 0xff) + 1
result = wide & 0xff
borrow = not bool(wide & 0x100)
overflow = bool((a ^ b) & (a ^ result) & 0x80)
exact = signed_byte(a) - signed_byte(b)
assert borrow == (a < b)
assert overflow == (not -128 <= exact <= 127)
print("65536 subtractions passed")
Using the addition overflow expression unchanged for subtraction would fail this test. The arithmetic operation, stored width, and interpretation all belong in the check’s name or documentation so a later caller cannot mix the contracts.
Check before narrowing in application code
A wider intermediate makes the intended policy easier to express when its own bounds are known. The sum of two signed eight-bit values lies between negative 256 and positive 254. A C implementation’s ordinary int type can represent that range, so promoting both operands before addition avoids signed overflow in the intermediate.
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
bool add_checked_i8(int8_t a, int8_t b, int8_t *out) {
int sum = (int)a + (int)b;
if (sum < INT8_MIN || sum > INT8_MAX) {
return false;
}
*out = (int8_t)sum;
return true;
}
Sections 5.2.4.2.1 and 7.20.1.1 of the C11 draft give the minimum int range and the exact-width type contract. This function assumes that the implementation supplies int8_t and that out points to writable storage. On failure it leaves the destination unchanged. That explicit behavior is useful to a caller: a failed check does not quietly publish a wrapped result. The cast occurs only after the value is proved representable in the destination type.
A larger arithmetic expression needs a new bound. Summing a thousand products can exceed a type that safely holds each individual product. For an inference accumulator or a byte-count calculation, bound the entire expression, including bias terms or offsets, before choosing the intermediate representation.
Extend the lesson into a small ALU
For a reproducible next exercise, add zero and negative status outputs to the masked result. Zero is true when all eight result bits are zero; negative is the stored sign bit. Check the four additions in the table again and record these two statuses alongside carry and overflow. In 127 plus one, the negative status is true even though the mathematical sum is positive: it describes the stored result, while overflow warns that its signed interpretation is unsuitable for the exact answer.
Nand2Tetris Project 2 makes a useful follow-on exercise because its ALU project separates the main result from the zero and negative status outputs, with supplied comparison files for both stages. Those two statuses do not by themselves provide a signed overflow flag. Ben Eater’s ALU project page presents a concrete add/subtract unit and links the component data sheets; use the page and schematic to connect the arithmetic to a buildable circuit, then consult the component specifications for electrical limits. The finite Python checks here are not measurements of that board.
Decide what should happen after detection
A checksum often intends modular arithmetic. A physical quantity such as a temperature usually needs a different policy. Wrapping a large positive sensor value to a negative number can conceal an invalid input. Saturation would clamp to the nearest endpoint; checked arithmetic would report failure; a wider representation would preserve the result if its range is sufficient.
Saturation also loses information. After several values clamp to 127, their original magnitudes cannot be recovered from the stored bytes. This tradeoff is useful in some quantized inference kernels and damaging in an accounting calculation. Overflow detection supplies evidence for a policy; it does not choose the policy for you.
The Computation Structures combinational logic notes connect binary addition to gates and carry propagation. At that level, two’s complement addition reuses the same adder as unsigned addition. The practical work sits at the boundary: retain the width, interpret the result consistently, and choose an explicit response when the mathematical answer does not fit.