Search

A technology, a device or an article

One CPU addition can signal two kinds of overflow

CF and OF describe different limits of one addition. Carry chains, signed range and ADD/ADC show why hardware flags must be interpreted through the program’s number model.

A row of red paper squares, an extra yellow square and two paper flags
ITNWS AI-generated conceptual illustration. A paper metaphor for a fixed-width result and separate arithmetic flags; not a CPU schematic.ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

FF plus 01 gives 00. This eight-bit addition answer is easy to present as evidence of broken arithmetic. But the processor retained the least significant eight bits, and reported the ninth separately. If we consider the inputs to be unsigned, we have 255 plus 1, the result of which is indeed outside the byte range. If the same bits are considered signed in two’s complement representation, it is minus one plus one. Zero is completely correct, there is no signed overflow. One machine operation leaves information for two interpretations, and the dispute begins where the program chose the wrong one.

The phrase "CPU detects overflow" is therefore too short. What exactly overflowed: an unsigned range, a signed range, an application counter with a special limit, or a buffer, the size of which is then calculated based on the resulting number? The first two questions have different architectural flags. The rest require application rules. You cannot get a ready answer about everything at once from the fact that the arithmetic block can set flags. It is more useful to ask what mathematical statement the program wants to check after addition and whether the chosen flag corresponds to it.

On x86, a regular ADD generates, among other things, CF and OF. The first indicates a carry beyond the most significant bit of the unsigned result, the second indicates the impossibility of representing the exact signed sum in the selected width. Intel Software Developer’s Manual Volume 2 describes both flags alongside the operation. Reviewed November 3, 2026, revision 325383-093US is dated September 2026. The rules themselves have existed for a long time: this is an explanation of CPU arithmetic, and not news about the appearance of a new instruction or a result from processor tests by ITNWS.

Let’s look at FF+01 again, now with explicit names. The low eight-bit result is 00, CF is 1, OF is 0. The unsigned sum 256 does not fit in the range 0 to 255. The signed sum 0 fits in the range −128 to 127. The flags are not contradictory: they answer different questions about the same bit operation. To require them to be equal would be to require that two different sets of admissible numbers have the same boundary.

Now add 7F and 01. The result is 80, CF is 0, OF is 1. In the unsigned interpretation, 127 + 1 = 128, the limit of 255 has not been reached. In the signed interpretation, positive 127 is increased to 128, which is not present in the eight-bit signed range; the bit pattern 80 represents −128. There was no carry beyond the eighth bit, but the signed interpretation overflowed. This is the second counterexample against the habit of calling CF a universal signal of any overflow. It is not required to notice the violation of the boundary for which another flag is intended.

Here it is convenient to note the direction of the sign error. When adding positive operands, a negative result indicates an exit from the positive half of the signed range. When adding negative ones, a positive result can indicate a way out on the other side. For operands of different signs, the usual exact sum remains between them and signed overflow does not occur. This reasoning applies to fixed width and two’s complement representation. It explains OF, but does not replace careful reading of the semantics of each other instruction, which also changes the flags register.

A zero result also does not solve anything in itself. In the first example, ZF is set because the stored bits are zero. Is the mathematical answer equal to zero? For signed interpretation yes, for unsigned exact sum no. The processor does not write a hidden philosophical assessment of the calculation into ZF: it communicates a certain property of the result of its operation. Likewise, SF reflects the most significant bit, rather than certifying that the application works with signed numbers at all. The context of the numerical model remains the responsibility of the program.

A carry is useful precisely because it crosses a word boundary

A large integer may have more bits than the register width. The carry then becomes a necessary input to the next part of the calculation. Let the illustrative unsigned number consist of two eight-bit words: the high word is 12 and the low word is FF, the integer notation is 12FF. Let’s add 0001. The low addition gives 00 with a carry, the high one should get 12 + 00 + 1 = 13. The full answer is 1300. If you process both halves with an independent ADD, you get 1200: exactly the carry that the first result could not accommodate is lost.

For such a dependency there is ADC, add with carry. It adds the operands and the input CF, storing the new result and new arithmetic flags. A typical chain begins with ADD on the low word and continues with ADC on the high ones. This is a dependency description, not a prescribed compiler output for any language or type. A real program must take into account the chosen width, the available operations, and its own representation of a large number. But the mathematical obligation is the same: the weight of the carry unit is equal to the radix of the low word, and not an arbitrary addition to any convenient field.

In the tutorial example, the base is 256. The entry 12FF means 0x12·256 + 0xFF, that is, 4863. After adding one we get 4864, corresponding to 0x13·256. This is why the carry is added to the adjacent high word. For 64-bit parts the radix is different, but the rule does not change. This calculation explains the correctness of multiword arithmetic; it does not measure its throughput and does not say anything about how many nanoseconds an operation will take on a particular CPU.

Between ADD and the next ADC, you cannot thoughtlessly insert a command that changes CF. If an unrelated comparison overwrites the useful carry in CF, the chain will receive a different input. In assembly, this dependency must be preserved explicitly; in a high-level language it is expressed using appropriate means, and then it is checked to see what code is actually built. Even an instruction that does not change the value of the desired register can be significant due to flags. The list of explicit operands does not always contain all the dependencies that the next machine operation uses.

An illustrated mechanism with two trays, dark beads and a copper bead on a lever
ITNWS AI-generated conceptual illustration. An image of carrying one unit into a higher word. The bead counts are not a binary example or a measurement. — ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

This, however, does not imply advice to write every long addition by hand. The Microsoft intrinsics list for x64 has the _addcarry family, which allows you to represent a carry as an input and output of an interface. GCC documents built-in overflow checking operations and carry-add operations. The choice depends on the required portability and build environment. The mere presence of an intrinsic does not prove the best machine code, and a familiar name does not eliminate the need to check the type of the result and the contract for the returned flag.

It is especially important not to conflate the flag from the last word with a guarantee about the entire number. In an unsigned chain, the final carry indicates that the sum has exceeded the entire allocated multiword width. If the representation allows one more word, it can be saved; if the width is fixed, the program needs to decide how to handle overflow. A separate OF on the intermediate part is not a universal overflow signal for the entire large signed number. The intermediate word serves bitwise arithmetic, and the signed interpretation of the whole is determined by its complete representation and upper bound.

There is no need to argue with hardware flags. The same applies to the program language, if you first read its rules. In C++ working draft on fundamental types, unsigned arithmetic is defined modulo a power of two corresponding to the width of the type. Signed overflow has no such general guarantee: it has undefined behavior. The fact that a particular ADD leaves the low-order bits and OF does not give the original program the right to first perform an illegal signed addition and then expect a portable check of the result by an ordinary comparison.

There is also a less noticeable catch: narrow types in expressions can be subject to integer promotions. Two byte-sized variables do not need to add together as eight-bit machine operands. The expression can be evaluated in a wider type, and the width will be reduced during subsequent conversion. Therefore, the initial examples describe the operation of the selected width, but do not promise the exact instruction for any expression with an unsigned char. Between the bit model, the type of the expression, and where the result is stored, there are sometimes three different boundaries of representability.

An application limit also matters for the buffer size. Let’s say the format allows a length less than a certain maximum, although a machine unsigned type can accommodate more. CF may remain zero if this rule is violated. Conversely, a hashing algorithm may intentionally use modular addition, for which carry is not an error at all. Treating every set CF as a failure is as misleading as ignoring it in a calculation where an exact unbounded sum is required. The error is defined by the task contract, and the flag provides only part of the required evidence.

Conditional branches after the comparison reflect a similar difference. The machine sequence for an unsigned relation uses different flag combinations than for a signed one. It cannot be concluded from this that the register stores a secret signedness label: the interpretation is chosen by the language operation and the corresponding sequence of instructions. The same bits allow both readings. When the debugger shows a negative number instead of a large positive one, you should first check the display format and type, and not assume that the processor changed the meaning of the data without a program command.

GCC’s checked overflow operation formulates the problem differently than naive addition followed by correction. The arguments are considered with infinite signed precision, the mathematical result is converted to the destination type, and the return boolean value indicates that the exact result does not match the stored one. Therefore, behavior is defined for it on inputs that would violate the range of a conventional signed expression. This is a useful example of an interface that first defines the desired mathematical operation and then allows the compiler to select appropriate hardware flags. Its contract cannot be reduced to a promise to always read one specific flag.

And the flag itself is not a persistent record. The following arithmetic instructions can replace it, the function call does not promise to preserve the previous CF for the caller, and the debug stop shows only the specific moment of execution. If the program needs to pass the flag onward as data, it is obtained by the specified operation and stored in a suitable representation. Looking at the flag register after a whole sequence of calculations and assigning it to the first line is a timing error. To analyze the dependency, you need to know which command last defined the flag being read and whether there was a new definition between the producer and the consumer.

Finally, carry and saturation are different operations. Modular addition preserves the remainder, while saturated addition leaves the extreme permissible value when it goes beyond the limit. Conventional ADD does not select saturation as intended by the author. If the format requires such a result, the algorithm must express a check and a boundary selection rule, or use a suitable special operation with known semantics. None of these flags allows us to obtain a zero and then call it a saturated maximum.

Performance comes later. A long chain of carries contains a sequential dependency: the higher part needs the result of the lower part. The processor’s ability to execute other instructions in parallel does not negate this particular relationship. There are special operations and techniques for organizing arithmetic, but they should be compared on certain algorithms, sizes and CPUs. One word ADD in a listing is not a unit of useful performance. It is also impossible to deduce the speed of large-integer arithmetic from an advertised clock rate without taking into account the dependence between its parts.

All numerical examples here are calculated analytically and use an explicitly specified width. They allow you to check the meaning of the flags, rather than rank processors. For the eight-bit FF + 01, two statements remain true at the same time: the unsigned exact sum did not fit, the signed one did. For 7F+01 the opposite is true. Responsible code preserves exactly the distinction that the task requires. CPU provides bits and flags; the program itself must choose the numerical model in which this information will become the correct solution.

Sources & documentation

  1. Intel SDM Volume 2, 325383-093US, September 2026 ↗
  2. Microsoft x64 intrinsics list ↗
  3. GCC Integer Overflow Builtins ↗
  4. C++ working draft: fundamental types ↗