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.
- During Addition: The Carry Flag is set to
1when the sum produces a carry out of the most significant bit (MSB). This indicates that the true mathematical result is too large to fit in the destination register (i.e., it exceeds \(2^N - 1\)). - During Subtraction: The Carry Flag acts as a
“borrow flag” in many architectures (such as x86). It is set to
1when a larger unsigned number is subtracted from a smaller unsigned number, requiring a borrow that cannot be satisfied within the bit width.
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.
- Addition Rule: An overflow occurs only when adding
two numbers of the same sign produces a result with the opposite sign
(e.g.,
positive + positive = negativeornegative + negative = positive). Adding numbers of opposite signs can never produce a signed overflow. - Hardware Logic: At the circuit level, the Overflow Flag is evaluated by calculating the XOR of the carry into the most significant bit (\(C_{in}\)) and the carry out of the most significant bit (\(C_{out}\)): \[\text{OF} = C_{in} \oplus C_{out}\]
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)
- Unsigned Perspective: \(127 + 1 = 128\). The value \(128\) fits comfortably in the \(0\) to \(255\) range. There is no carry-out from the MSB. \(\rightarrow\) CF = 0
- Signed Perspective: \((+127) + (+1) = -128\). Adding two positive numbers resulted in a negative value because the sum exceeded \(+127\). \(\rightarrow\) OF = 1
Example
2: 0xFF + 0x01 (11111111 +
00000001 = 00000000 with carry)
- Unsigned Perspective: \(255 + 1 = 256\). The value \(256\) cannot fit into 8 bits (\(0\text{--}255\)), producing a carry out of the MSB. \(\rightarrow\) CF = 1
- Signed Perspective: \((-1) + (+1) = 0\). The value \(0\) fits correctly within the \(-128\) to \(+127\) range, and the result is mathematically sound. \(\rightarrow\) OF = 0
Example
3: 0x80 + 0x80 (10000000 +
10000000 = 00000000 with carry)
- Unsigned Perspective: \(128 + 128 = 256\). The value exceeds \(255\), generating a carry-out. \(\rightarrow\) CF = 1
- Signed Perspective: \((-128) + (-128) = -256\). The value falls below \(-128\), flipping the sign to \(0\) (positive). \(\rightarrow\) OF = 1
Summary Rule
- Use the Carry Flag (CF) when working with unsigned
types (
uint8_t,uint32_t, pointer arithmetic, multi-precision math). - Use the Overflow Flag (OF) when working with signed
types (
int8_t,int32_t, two’s complement calculations).