Skip to content

Computer Architecture

What are CPU flags? Zero, carry and the status register

CPU flags are single bits a processor sets after each calculation to describe the answer: Z (zero) when the result is 0, C (carry) when it was too big to fit, N (negative) when its top bit is 1, and V (overflow) when a signed result came out with the wrong sign. They live in the status register, and the next instruction reads them to decide what to do.

Carry: the answer didn't fit

Two bits hold 0 to 3. Add 3 + 1 and the true answer, 4, is 100 in binary: the sum shows 00 and the 1 comes out of the top column as carry. Without the flag you'd never know the answer wasn't 0. It's the same at any width: in 8 bits, 255 + 1 gives 0 with C set.

Zero: one NOR

ZERO = NOT (S1 OR S0)

A NOR gives 1 only when every input is 0. Across the sum bits, it lights for any result of 0, however it came about: 0 + 0, 1 + 3, 2 + 2 and 3 + 1 all light it in a two-bit adder. A wider processor uses a wider NOR, one input per result bit.

Live experiment

An adder's warning lamps

Two 2-bit numbers go into an adder. SUM keeps the low two bits, CARRY lights when the answer needs a third, and ZERO, a NOR across the sum bits, lights when SUM shows 0. Try every pair and find all four ways to light ZERO: three of them are really 4.

Try it yourself: free lesson

“Raise a flag” is lesson 1 of 6 in Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 14 before it first.

Negative and overflow

Signed numbers in two's complement use the top bit as the sign, and N simply copies it. V is not the same as C. In 8 bits, 127 + 1 = 128, which as a signed byte reads −128: V is set because the sign came out wrong, while C stays clear because nothing came out of the top. Code working with unsigned numbers tests C; code working with signed numbers tests V.

Flags make decisions

A flag is set by one instruction and read by the next, often a few billionths of a second later. if (a == b) usually compiles to a subtraction, a − b, followed by a branch that tests Z, because equal numbers give 0. A counted loop ends with "jump if not zero": take one away, and go round again while Z is clear. Every decision a processor makes comes down to testing a bit like these.

Where they live

ARM calls its set NZCV. Intel's x86 keeps them in EFLAGS, RFLAGS on 64-bit chips, as ZF, CF, SF and OF. The AVR chip on an Arduino Uno keeps them in SREG, with Z, C, N and V among its bits. A compare instruction such as CMP or CP is a subtraction that throws the answer away and keeps only the flags.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Raise a flag” is lesson 1 of 6 in Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 14 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The adder's ZERO flag lights when…
    Show the answer

    Answer: Every sum bit is 0. ZERO is a NOR across the sum bits. 1 + 3 lights it too: the sum shows 0.

    • Why not “Any sum bit is 0”? That would light for a sum like 10, which isn't zero. ZERO needs every sum bit at 0.
    • Why not “CARRY is 1”? CARRY can be 1 with a sum that isn't zero: 3 + 2 shows 01 with CARRY lit.
    • Why not “The inputs are both 0”? Zero inputs do give a zero sum, but so do 1 + 3 and 2 + 2. ZERO looks at the sum, not the inputs.
  2. Question 2 of 3ZERO = NOT (A1 OR A0). It's 1 when…
    Show the answer

    Answer: Both bits are 0. The OR is 0 only when both bits are 0, and the NOT turns that into a 1.

    • Why not “Both bits are 1”? With both bits 1 the number is 3. The OR is 1, so ZERO is 0.
    • Why not “Either bit is 0”? With one bit 0 and the other 1, the OR is 1 and the NOT makes ZERO 0. The number isn't zero.
    • Why not “A1 is 0”? A1 at 0 with A0 at 1 is the number 1, and ZERO stays 0. Both bits must be 0.
  3. Question 3 of 3ZERO built from NOT A1 alone. A = 01 gives…
    Show the answer

    Answer: ZERO wrongly 1: it never looked at A0. NOT A1 is 1 whenever the high bit is 0, including for 01, which isn't zero. Both bits have to go into the OR.

    • Why not “EQ 1”? EQ is a separate output that compares two numbers. The broken gate here drives ZERO.
    • Why not “ZERO 0”? ZERO should be 0 for 01, but NOT A1 is 1 whenever the high bit is 0, and it never looks at A0.
    • Why not “ZERO 1, correctly”? 01 is 1, not zero, so a ZERO of 1 is wrong. Checking only the high bit misses the low one.
Keep practising: free lesson

“Raise a flag” is lesson 1 of 6 in Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 14 before it first.

Questions people ask

What is the status register in a CPU?

The register that holds the processor's flags, one bit each: zero, carry, negative, overflow and a few more. Arithmetic and compare instructions update it, and conditional branches read it. Intel's x86 calls it EFLAGS, ARM the APSR, and the AVR chip on an Arduino Uno SREG.

What is the difference between the carry flag and the overflow flag?

Carry says an unsigned result didn't fit: a 1 came out of the top bit. Overflow says a signed result came out with the wrong sign, like 127 + 1 reading as −128 in 8 bits. One addition can set either, both or neither, and the program tests whichever matches how it treats the numbers.

How does a CPU compare two numbers?

It subtracts one from the other and keeps only the flags. Equal numbers give zero, so Z is set. For unsigned numbers a borrow, shown in the carry flag, says the first was smaller. A conditional branch then tests the flag it needs.

Every topic