CPU Flags Register: Carry, Zero, Overflow, Sign
Learn how Z, C, N, and V are generated, why subtraction carry conventions differ, and how CPUs turn flags into correct signed and unsigned branches.
TL;DR: Z, N, C, and V are not four interchangeable kinds of “overflow.” Z and N describe the truncated result, C describes unsigned carry, and V describes signed overflow. Subtraction needs one extra decision: some instruction sets store borrow, while others store not-borrow. State that convention before deriving unsigned branch conditions.
A flags register is a small piece of state with a large effect on program flow. An ALU produces a result; flag-generation logic summarizes selected properties of that operation; and a later branch, add-with-carry, or conditional instruction reads those bits.
The idea is simple. The details are not. A design can produce the right subtraction result and still take the wrong unsigned branch if it confuses carry-out with borrow. It can also misclassify signed values if it tests the result’s sign bit without accounting for overflow.
This article develops a precise four-flag model for an -bit ALU, shows how to verify it with 8-bit examples, and explains where real instruction sets deliberately differ.
Flags Are Part of the Instruction-Set Contract
The labels Z, C, N, and V are common, but their behavior is defined by an architecture and by each instruction—not by the letters alone.
| Architecture | Where the state appears | Important distinction |
|---|---|---|
| x86-64 | selected bits in RFLAGS | SUB sets CF when an unsigned borrow occurs |
| AArch32 | condition flags in APSR/CPSR | subtract-with-flags uses C as not-borrow |
| AArch64 | N, Z, C, and V in PSTATE | many operations have separate flag-setting forms such as ADDS and SUBS |
| RISC-V base ISA | no arithmetic condition-code register | conditional branches compare registers directly |
| DigiSim teaching ALU | four outputs: Z, C, N, V | SUB sets C when an unsigned borrow occurs |
Even within one architecture, instructions do not necessarily update every flag. Some preserve a flag, some replace it, and some leave a flag architecturally undefined. The safe question is therefore not “What does an AND operation do to flags in general?” It is “What does this ISA specify for this instruction form?”
DigiSim’s FLAGS_REGISTER stores Z, C, N, and V inputs when its load input is asserted on a rising clock edge. It does not decide what those inputs mean. The surrounding ALU and control logic establish that contract.
A Precise Four-Flag Model
Let and be -bit operands, the -bit stored result, and its most significant bit.
Zero: Z
Z is 1 exactly when every result bit is 0:
Hardware usually implements this as a reduction NOR, often synthesized as a tree rather than one enormous physical gate.
For subtraction, precisely when the two -bit operands are equal. That is why a compare instruction can perform a subtraction, discard , and retain Z for an equality branch.
Negative: N
N copies the most significant result bit:
If is interpreted as an -bit two’s-complement value, that bit is its sign bit. However, N alone does not tell you whether the exact mathematical answer was negative. If signed overflow occurred, the truncated result can have the opposite sign. Signed comparisons consequently use , not N by itself.
Carry: C
For addition, define C as the carry out of the most significant bit:
It reports whether the exact unsigned sum is at least . For example, 8-bit produces and .
Subtraction is where terminology can mislead. An adder commonly computes
Let be that adder’s final carry-out. Then:
In words, a final carry means no borrow was required. Two equally valid architectural encodings follow:
- Borrow convention: . A set flag means . x86 CF follows this interpretation for
SUB. - Not-borrow convention: . A set flag means . Arm condition codes use this interpretation after a flag-setting subtraction.
The arithmetic circuit can be identical in both CPUs. Only the flag encoding and branch decoder differ.
The DigiSim 4-bit and 8-bit ALUs use the borrow convention after subtraction:
Thus C=1 exactly when unsigned . A custom design that exposes raw carry-out instead must invert the subtraction conditions derived later.
Signed overflow: V
V says that the exact signed result is outside the representable range
For addition:
Two operands with the same sign overflow when the stored result has the opposite sign.
For subtraction:
Operands with different signs overflow when the result’s sign differs from A’s sign.
If subtraction is implemented as , the familiar carry expression
also works, where enters the sign-bit adder and leaves it. The sign-bit formulas above are often easier to verify at the ALU boundary because they do not require exposing an internal carry.
Four 8-Bit Cases Worth Testing
Small ordinary values do not expose flag mistakes. Boundary cases do.
1. Ordinary addition: 8 + 8
0000 1000
+ 0000 1000
-----------
0001 0000
The result is 16, so , , , and .
2. Signed overflow without unsigned carry: 127 + 1
0111 1111 (+127)
+ 0000 0001
-----------
1000 0000 (-128 when interpreted as signed)
The 8-bit unsigned answer 128 is representable, so . The signed answer +128 is not representable, so . The stored result has its MSB set, giving , while .
This case demonstrates why C and V are independent.
3. Unsigned carry without signed overflow: 255 + 1
1111 1111
+ 0000 0001
-----------
1 0000 0000
The stored 8-bit result is zero: and . The ninth bit makes . Interpreted as signed values, this is , so .
4. Subtraction that borrows: 0 - 1
The adder evaluates :
0000 0000
+ 1111 1110
+ 1
------------
1111 1111
The final carry-out is 0, so DigiSim’s borrow-style C output is 1. The result flags are therefore , , , and .
This is the essential convention test. If your expected C value is not written down before simulation, both answers can look plausible.
Correct Branch Conditions After A - B
Suppose a compare computes and updates Z, N, C, and V.
Equality
Equality is independent of signedness:
| Relation | Condition |
|---|---|
Signed comparison
Signed less-than must correct the result sign when subtraction overflowed:
| Signed relation | Condition |
|---|---|
Consider 8-bit . The stored result is 1000 0000, so N is 1, but the exact result +128 is positive and overflowed, so V is also 1. correctly reports that 127 is not less than -1.
Unsigned comparison
With DigiSim’s borrow C convention:
| Unsigned relation | Condition |
|---|---|
With a raw-carry/not-borrow flag , the same rows become , , , and .
This is why mixing branch mnemonics from different architectures is dangerous. Arm’s HS/CS after CMP corresponds to not-borrow C=1, while x86’s JB/JC after CMP corresponds to borrow CF=1. The words “carry set” are not enough; the preceding operation and ISA convention matter.
The Register and the Logic Around It
The flags register itself is only storage:
result[n-1:0] ── reduction NOR ── Z_next ─┐
result[n-1] ─────────────────── N_next ─┤
adder carry ── operation select ─ C_next ├─▶ FLAGS register ─▶ branch decoder
operand/result signs ───────────── V_next ─┤
clock + flags_load ────────────────────────┘
Three design choices belong outside the storage element:
- Generation: the ALU produces candidate values such as
Z_next,C_next,N_next, andV_next. - Selection: the opcode determines which candidate value applies. A shift’s carry may come from the shifted-out bit rather than an adder.
- Write policy: the control unit determines whether each flag is loaded, cleared, or preserved.
A simple teaching CPU can update all four flags after ADD, SUB, and CMP and preserve them after moves and memory operations. That is coherent as long as its ISA says so. Do not silently generalize that policy to a commercial processor. For example, AArch64 normally distinguishes ADD from flag-setting ADDS, and SUB from SUBS; x86 defines instruction-specific effects in its instruction reference.
The flags must also obey ordinary synchronous timing. Candidate inputs must meet setup and hold requirements around the active clock edge, and the branch decoder must receive stable registered outputs before its next capture point. Functional simulation can validate truth tables and control sequencing, but it does not replace static timing analysis for an implemented chip or FPGA.
Why RISC-V Does Not Need Arithmetic Flags
The RISC-V base integer ISA compares register operands as part of its conditional branches:
BEQ rs1, rs2, target
BNE rs1, rs2, target
BLT rs1, rs2, target # signed
BLTU rs1, rs2, target # unsigned
BGE rs1, rs2, target # signed
BGEU rs1, rs2, target # unsigned
There is no architectural condition-code state for an intervening instruction to overwrite. This is a different ISA contract, not a claim that comparison logic has disappeared: the branch still needs to establish the requested relation.
RISC-V can compare equality with zero by using its hardwired x0 register. Other constants may already reside in a register or may need to be materialized, depending on the surrounding code. Multiword arithmetic can form an explicit carry bit with unsigned comparisons such as SLTU.
Flags-based and compare-in-branch designs can both be implemented efficiently. The important lesson is that software-visible state and instruction semantics shape how arithmetic results reach control flow.
Build and Verify It in DigiSim
The FLAGS_REGISTER component accepts Z, C, N, and V as data inputs and captures them on a rising edge when LD is high. Build the surrounding logic in this order:
- Choose a width, such as 8 bits.
- Write the subtraction convention on the schematic: for DigiSim,
C after SUB = borrow = NOT raw carry-out. - Generate Z from a reduction NOR and N from the result MSB.
- Generate V with operation-specific addition and subtraction formulas.
- Select the correct C source for ADD and SUB.
- Assert LD only for instructions that your teaching ISA defines as flag-setting.
- Decode signed and unsigned branch conditions with separate logic.
Use a compact verification table rather than a few random inputs:
| Operation | A | B | Expected R | Z | C | N | V |
|---|---|---|---|---|---|---|---|
| ADD | 8 | 8 | 16 | 0 | 0 | 0 | 0 |
| ADD | 127 | 1 | 128 | 0 | 0 | 1 | 1 |
| ADD | 255 | 1 | 0 | 1 | 1 | 0 | 0 |
| SUB | 0 | 1 | 255 | 0 | 1 | 1 | 0 |
| SUB | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
| SUB | 127 | 255 | 128 | 0 | 1 | 1 | 1 |
| SUB | 128 | 1 | 127 | 0 | 0 | 0 | 1 |
Here C uses DigiSim’s documented borrow convention on subtraction. If a custom circuit intentionally exposes raw carry-out/not-borrow instead, invert the subtraction C column and the unsigned decoder.
Then test state behavior: keep LD low while changing the ALU inputs and confirm that stored flags do not change; pulse LD and confirm that all enabled flags capture together; and execute compare–non-flag-setting operation–branch to verify that the comparison flags persist.
Design Checklist
Before connecting flags to the program counter, confirm:
- The subtraction carry convention is explicit.
- C and V are tested as independent properties.
- Signed less-than uses , not N alone.
- Unsigned comparisons use C with the correct polarity.
- Every opcode has a documented flag-write policy.
- The flags register stores inputs; it does not infer ALU semantics.
- Boundary vectors include zero, all ones, maximum positive, and minimum negative.
- Simulation claims are limited to logic and sequencing unless physical timing has also been analyzed.
Once those conditions hold, a four-bit status register becomes a dependable bridge between the ALU, the control unit, and the program counter rather than a source of architecture-dependent surprises.



