Carry Flag vs Overflow Flag in Binary Arithmetic

The Carry Flag (CF) and the Overflow Flag (OF) are processor status flags used to determine whether the result of a binary arithmetic operation exceeds the storage capacity of a fixed-width register. The fundamental conceptual difference between them lies in the mathematical interpretation of the binary data: the Carry Flag detects overflow in unsigned arithmetic, while the Overflow Flag detects overflow in signed (two’s complement) arithmetic. Because the central processing unit (CPU) executes identical binary addition and subtraction regardless of sign convention, it computes and sets both flags simultaneously, leaving the software to inspect the flag relevant to its data type.

The Carry Flag (CF): Unsigned Arithmetic

The Carry Flag tracks arithmetic operations treating values strictly as non-negative integers ranging from \(0\) to \(2^N - 1\), where \(N\) is the bit width of the register.

The Overflow Flag (OF): Signed Arithmetic

The Overflow Flag tracks arithmetic operations treating values as signed integers encoded in two’s complement, representing a range from \(-2^{N-1}\) to \(2^{N-1} - 1\).

In two’s complement, the most significant bit represents the sign (\(0\) for positive, \(1\) for negative). A signed overflow occurs when an operation produces a result that falls outside the representable positive or negative range, causing the sign bit to flip incorrectly.

Comparison with 8-Bit Examples

An 8-bit register stores unsigned values from \(0\) to \(255\) and signed values from \(-128\) to \(+127\). The following examples demonstrate how identical binary operations yield different flag states.

Example 1: 0x7F + 0x01 (01111111 + 00000001 = 10000000)

Example 2: 0xFF + 0x01 (11111111 + 00000001 = 00000000 with carry)

Example 3: 0x80 + 0x80 (10000000 + 10000000 = 00000000 with carry)

Summary Rule