Skip to content
BetterDL

Status flags

Also called: flags, condition codes, NZCV, condition flags, flag bits

Single-bit ALU outputs that describe a result: N (negative), Z (zero), C (carry-out) and V (signed overflow). Programs test them to compare and branch.

After each operation, an ALU produces a few extra bits that describe its result R. These status flags (also called condition codes) let a program ask "was it zero?" or "did it overflow?" without examining the result itself.

For an n-bit result, the four standard flags are:

Where they come from matters. N and Z describe the value the ALU outputs, so they're taken from the result MUX output. C and V only exist inside the adder, so they come from the carry chain and only mean something after ADD or SUB.

In this course C is the raw carry-out, so after SUB, C = 1 means "no borrow". ARM processors work this way; x86 stores the inverted bit as a borrow.

The flags turn subtraction into comparison. Compute A − B and read them: Z = 1 means equal, C = 0 means unsigned A < B, and = 1 means signed A < B. See comparison by subtraction.

R3R2R1R0Z

Worked examples

Example

Flags for 1011 + 0111

4-bit addition, 11 + 7.

  1. 1.

    Bit 0: 1 + 1 → 0, carry 1. Bit 1: 1 + 1 + 1 → 1, carry 1.

  2. 2.

    Bit 2: 0 + 1 + 1 → 0, carry 1, so C3 = 1. Bit 3: 1 + 0 + 1 → 0, carry 1, so C4 = 1.

  3. 3.

    R = 0010. N = 0, Z = 0, C = C4 = 1, V = C4 ⊕ C3 = 0.

  4. 4.

    NZCV = 0010. Unsigned 11 + 7 = 18 overflowed (C = 1). Signed −5 + 7 = 2 is correct (V = 0).

Example

Flags for 0001 − 0100

4-bit subtraction, 1 − 4, computed as 0001 + 1011 + 1.

  1. 1.

    Bit 0: 1 + 1 + 1 → 1, carry 1. Bit 1: 0 + 1 + 1 → 0, carry 1.

  2. 2.

    Bit 2: 0 + 0 + 1 → 1, carry 0, so C3 = 0. Bit 3: 0 + 1 + 0 → 1, carry 0, so C4 = 0.

  3. 3.

    R = 1101 = −3. N = 1, Z = 0, C = 0 (a borrow: 1 < 4), V = 0.

  4. 4.

    NZCV = 1000.

Common mistakes

  • Using C as the signed overflow flag. C is for unsigned; V is for signed.

  • Computing Z from the adder's sum instead of the ALU's output.

  • Thinking N = 1 means something went wrong. Negative results are normal.

Practice Status flags

Interactive questions with instant feedback and a worked solution for every wrong answer.

Learn it step by step

Status flags is taught in Adders and ALUs and Basic CPU / Computer Architecture.