Difference Between Overflow Flag (OF) and Carry Flag (CF): Meaning + Examples
In typical CPU instruction-sets (notably x86), the Carry Flag (CF) and the Overflow Flag (OF) both relate to “incorrect results” but for different interpretations of the data.
- Carry Flag (CF) is about unsigned magnitude: did the operation generate a carry out (addition) or a borrow (subtraction) beyond the fixed bit-width?
- Overflow Flag (OF) is about signed range (two’s complement): did the operation produce a result that is outside the representable signed range for the operand width?
Mermaid intuition: CF vs OF correspond to different “truth conditions” for the same bit-pattern result.
Important: OF and CF can be set independently. That’s why learning their exact meaning matters for correctness in both signed and unsigned algorithms.
Flags tutorial: Carry vs Overflow (practical examples)
Key definitions (x86-style semantics)
Carry Flag (CF)
- For addition of -bit operands, CF is set if the full (mathematically correct) sum exceeds —i.e., there is a carry out of the most significant bit.
- For subtraction, CF is set if a borrow is needed in the unsigned sense.
Overflow Flag (OF)
- For addition of two’s-complement signed -bit integers, OF is set if the signed result is outside .
- Equivalently: OF becomes 1 when adding two values with the same sign produces a result with the opposite sign (for fixed-width two’s complement).
Work an example in both signed and unsigned interpretations
- 1Step 1
Decide the operand size (let’s use 8-bit). Then results wrap modulo in the register.
- 2Step 2
Do the math using unbounded integers to see whether a carry out occurred (unsigned) and whether the signed range is violated.
- 3Step 3
CF=1 if there is a carry out beyond for addition, or a borrow for subtraction.
- 4Step 4
OF=1 if the signed two’s-complement result cannot be represented in bits (range violation).
- 5Step 5
It’s common to see: CF=1, OF=0; CF=0, OF=1; or both 1—depending on the operands.
Example 1 (Carry set, Overflow not set): unsigned overflow, but signed still valid
Let’s use 8-bit addition:
- Operands: and
- Binary:
- unsigned, signed (two’s complement 8-bit)
- unsigned, signed
Compute the raw sum
Registers keep only the low 8 bits:
Unsigned check → CF
Unsigned sum exceeded , so there is a carry out:
- CF = 1
Signed check → OF
Signedly: which is representable in 8-bit signed range:
- OF = 0
So this produces:
- Result:
- CF = 1
- OF = 0
Mermaid summary:
Example 2 (Overflow set, Carry not set): signed overflow, but unsigned still in range
Use 8-bit addition:
- Operands: and
- Interpretations:
- unsigned, signed
- unsigned, signed
Compute the raw sum
In 8-bit registers:
Unsigned check → CF
Unsigned maximum is , and , so no carry out:
- CF = 0
Signed check → OF
8-bit signed range is . But the mathematical signed result is , which is out of range:
- OF = 1
So:
- Result:
- CF = 0
- OF = 1
Mermaid summary:
Rule of thumb
Think of CF as unsigned carry/borrow, and OF as signed range overflow. Same bit result, different meaning depending on whether you treat operands/results as signed or unsigned.
Don’t use the wrong flag for comparisons
If you’re doing unsigned arithmetic, CF/CF-based logic is relevant; for signed arithmetic, OF (plus sign bit) matters. Using the wrong flag often breaks edge cases like or .
How to decide CF vs OF during debugging
Unsigned vs signed
1) Identify signedness intentAre you interpreting operands as unsigned (0..) or signed two’s complement (..)?"
Look for CF
2) Check carry/borrowCF indicates carry/borrow out of the -bit boundary."
Look for OF
3) Check signed rangeOF indicates whether the signed result can’t be represented in bits."
Test with edges
4) Validate with a concrete exampleTry patterns near and to confirm understanding."
Flag behavior for two 8-bit addition cases
Which flags are set for each example.
Common questions
Carry vs Overflow Quick Checks
Knowledge Check
For 8-bit addition 0xFF + 0x01, what are CF and OF?
Explore Related Topics
Differences Between Leaky Bucket and Token Bucket Algorithms (Traffic Shaping vs Rate Limiting)
8085 Microprocessor Flags: Correct Answer and Conceptual Explanation
The 8085 microprocessor’s flag register contains five active status flags that are automatically set after arithmetic and logical operations.
- The 8‑bit flag register uses only five bits: Sign (S), Zero (Z), Auxiliary Carry (AC), Parity (P), and Carry (CY).
- These flags guide conditional branch instructions such as jump‑on‑zero or jump‑on‑carry.
- Although the register is 8 bits wide, the remaining three bits are unused/reserved, a common source of exam mistakes.
- The Auxiliary Carry flag is especially important for BCD arithmetic.
Process in Operating Systems and the Contrast Between Interprocess Communication Models
A process is the active execution of a program with its own state, resources, and PCB, and operating systems use it for scheduling, protection, and cooperation via interprocess communication (IPC); the two primary IPC models—shared memory and message passing—differ in data movement, synchronization, kernel involvement, and suitability for local versus distributed use.
- PCB stores a process’s state, registers, scheduling info, and resource data, enabling context switches.
- Standard process lifecycle: new → ready → running → waiting/blocked → terminated.
- IPC is required for cooperating processes because each has an isolated address space.
- Shared memory offers high performance for large local data but demands explicit synchronization.
- Message passing provides easier, safer communication and works well across machines, at the cost of higher kernel overhead.