← Back to blogCPU architecture

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.

DennyDenny
10 min read
Chalkboard feature illustration for “CPU Flags Register: Carry, Zero, Overflow, Sign”

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 nn-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.

ArchitectureWhere the state appearsImportant distinction
x86-64selected bits in RFLAGSSUB sets CF when an unsigned borrow occurs
AArch32condition flags in APSR/CPSRsubtract-with-flags uses C as not-borrow
AArch64N, Z, C, and V in PSTATEmany operations have separate flag-setting forms such as ADDS and SUBS
RISC-V base ISAno arithmetic condition-code registerconditional branches compare registers directly
DigiSim teaching ALUfour outputs: Z, C, N, VSUB 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 AA and BB be nn-bit operands, RR the nn-bit stored result, and Rn−1R_{n-1} its most significant bit.

Zero: Z

Z is 1 exactly when every result bit is 0:

Z=R0∨R1∨⋯∨Rn−1‾Z = \overline{R_0 \lor R_1 \lor \cdots \lor R_{n-1}}

Hardware usually implements this as a reduction NOR, often synthesized as a tree rather than one enormous physical gate.

For subtraction, A−B=0A-B=0 precisely when the two nn-bit operands are equal. That is why a compare instruction can perform a subtraction, discard RR, and retain Z for an equality branch.

Negative: N

N copies the most significant result bit:

N=Rn−1N = R_{n-1}

If RR is interpreted as an nn-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 N⊕VN \oplus V, not N by itself.

Carry: C

For addition, define C as the carry out of the most significant bit:

Cadd=cnC_{\text{add}} = c_n

It reports whether the exact unsigned sum is at least 2n2^n. For example, 8-bit 255+1255+1 produces R=0R=0 and Cadd=1C_{\text{add}}=1.

Subtraction is where terminology can mislead. An adder commonly computes

A−B=A+B‾+1A-B = A+\overline{B}+1

Let CrawC_{\text{raw}} be that adder’s final carry-out. Then:

Craw=1  ⟺  A≥B (unsigned)C_{\text{raw}}=1 \iff A \ge B \text{ (unsigned)}

In words, a final carry means no borrow was required. Two equally valid architectural encodings follow:

  • Borrow convention: Bflag=Craw‾B_{\text{flag}}=\overline{C_{\text{raw}}}. A set flag means A<BA<B. x86 CF follows this interpretation for SUB.
  • Not-borrow convention: Csub=CrawC_{\text{sub}}=C_{\text{raw}}. A set flag means A≥BA\ge B. 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:

Csub=Craw‾C_{\text{sub}}=\overline{C_{\text{raw}}}

Thus C=1 exactly when unsigned A<BA<B. 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

−2n−1≤x≤2n−1−1-2^{n-1} \le x \le 2^{n-1}-1

For addition:

Vadd=An−1⊕Bn−1‾∧(An−1⊕Rn−1)V_{\text{add}} = \overline{A_{n-1}\oplus B_{n-1}} \land (A_{n-1}\oplus R_{n-1})

Two operands with the same sign overflow when the stored result has the opposite sign.

For subtraction:

Vsub=(An−1⊕Bn−1)∧(An−1⊕Rn−1)V_{\text{sub}} = (A_{n-1}\oplus B_{n-1}) \land (A_{n-1}\oplus R_{n-1})

Operands with different signs overflow when the result’s sign differs from A’s sign.

If subtraction is implemented as A+B‾+1A+\overline B+1, the familiar carry expression

V=cn−1⊕cnV=c_{n-1}\oplus c_n

also works, where cn−1c_{n-1} enters the sign-bit adder and cnc_n 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 Z=0Z=0, N=0N=0, C=0C=0, and V=0V=0.

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 C=0C=0. The signed answer +128 is not representable, so V=1V=1. The stored result has its MSB set, giving N=1N=1, while Z=0Z=0.

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: Z=1Z=1 and N=0N=0. The ninth bit makes C=1C=1. Interpreted as signed values, this is −1+1=0-1+1=0, so V=0V=0.

4. Subtraction that borrows: 0 - 1

The adder evaluates 0+11111110+10+\mathtt{11111110}+1:

   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 Z=0Z=0, C=1C=1, N=1N=1, and V=0V=0.

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 R=A−BR=A-B and updates Z, N, C, and V.

Equality

Equality is independent of signedness:

RelationCondition
A=BA=BZZ
A≠BA\ne BZ‾\overline Z

Signed comparison

Signed less-than must correct the result sign when subtraction overflowed:

Signed relationCondition
A<BA<BN⊕VN\oplus V
A≥BA\ge BN⊕V‾\overline{N\oplus V}
A≤BA\le BZ∨(N⊕V)Z\lor(N\oplus V)
A>BA>BZ‾∧N⊕V‾\overline Z\land\overline{N\oplus V}

Consider 8-bit 127−(−1)127-(-1). The stored result is 1000 0000, so N is 1, but the exact result +128 is positive and overflowed, so V is also 1. N⊕V=0N\oplus V=0 correctly reports that 127 is not less than -1.

Unsigned comparison

With DigiSim’s borrow C convention:

Unsigned relationCondition
A<BA<BCC
A≥BA\ge BC‾\overline C
A≤BA\le BC∨ZC\lor Z
A>BA>BC‾∧Z‾\overline C\land\overline Z

With a raw-carry/not-borrow flag Craw=C‾C_{\text{raw}}=\overline C, the same rows become Craw‾\overline{C_{\text{raw}}}, CrawC_{\text{raw}}, Craw‾∨Z\overline{C_{\text{raw}}}\lor Z, and Craw∧Z‾C_{\text{raw}}\land\overline Z.

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:

  1. Generation: the ALU produces candidate values such as Z_next, C_next, N_next, and V_next.
  2. Selection: the opcode determines which candidate value applies. A shift’s carry may come from the shifted-out bit rather than an adder.
  3. 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:

  1. Choose a width, such as 8 bits.
  2. Write the subtraction convention on the schematic: for DigiSim, C after SUB = borrow = NOT raw carry-out.
  3. Generate Z from a reduction NOR and N from the result MSB.
  4. Generate V with operation-specific addition and subtraction formulas.
  5. Select the correct C source for ADD and SUB.
  6. Assert LD only for instructions that your teaching ISA defines as flag-setting.
  7. Decode signed and unsigned branch conditions with separate logic.

Use a compact verification table rather than a few random inputs:

OperationABExpected RZCNV
ADD88160000
ADD12711280011
ADD255101100
SUB012550110
SUB1101000
SUB1272551280111
SUB12811270001

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 N⊕VN\oplus V, 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.

References

Apply the concept in a simulator model.