Carry FlagEdit
The carry flag (CF) is a fundamental status indicator in many central processing units, signaling when an unsigned arithmetic operation produces a carry out of the most significant bit or when a subtraction requires a borrow. In practice, this flag enables reliable, multi-word arithmetic and makes possible a range of conditional operations that depend on the result of prior calculations. While the exact semantics can vary by architecture, the core idea is simple: track carry between consecutive arithmetic steps so higher-precision math and decision-making based on numeric results remain efficient and correct. The carry flag is closely tied to the notion of unsigned arithmetic, and it is often contrasted with the overflow flag, which captures signed overflow rather than carry in unsigned terms. flag register mechanisms hold the CF alongside other condition codes that guide branches and instructions like ADC and SBB.
Historically, carry-tracking capabilities emerged with the first practical microprocessors and have remained a core part of how modern CPUs handle multi-precision arithmetic. Early designs in 8-bit microprocessors introduced a dedicated bit in the status register to record carry, enabling programmers to implement arithmetic across multiple words without repeatedly invoking a full multi-word routine. This design approach persisted into later families such as Intel 8080 and successors, where the carry flag became a standard tool for building more capable arithmetic and I/O routines. Over time, variants of the same concept appeared in other families, reinforcing a shared pattern across the industry. status flag and conditional jump depend on this flag to decide the flow of control after arithmetic.
Technical details
Semantics - In unsigned addition, the carry flag is set if the sum exceeds the maximum value representable in the destination width. For an 8-bit example, 0xFF + 0x02 yields 0x01 with CF set to 1. - In subtraction, the carry flag is set if no borrow is required (i.e., the minuend is greater than or equal to the subtrahend). If a borrow occurs, CF is cleared. - The carry flag is separate from the signed overflow flag (OF). CF tracks unsigned carry/borrow, while OF tracks whether the signed result cannot be represented in the given width.
Interaction with other flags - The carry flag often interacts with the zero, sign, and parity flags to influence conditional instructions (for example, “jump if carry” or related conditions in various instruction sets). See how a processor exposes these condition codes in its condition codes. - In multi-precision arithmetic, the CF from a lower word is used as an input carry for the next higher word, enabling correct propagation across the full width of a multi-word value. - Some architectures provide explicit operations that incorporate carry across operands, such as add-with-carry and subtract-with-borrow, to simplify software that must manage large integers or implement decimal or binary-coded decimal routines. See Add with carry and Subtract with borrow for common examples.
Architectures and usage
x86 and x86-64 - The carry flag is bit 0 of the flag register and is used by a wide range of instructions. In addition to arithmetic, it controls the flow of a number of conditional jumps (for example, jc and jb variants) and can be queried or set with specific instructions (for example, setc). The x86 family provides dedicated instructions for performing arithmetic across word boundaries and propagating the carry into subsequent steps via adc and sbb instructions, which is essential for implementing multi-precision algorithms in software. See x86 assembly for a comprehensive view of how CF interacts with control flow and arithmetic.
ARM architecture - In ARM, the carry flag is stored in the C flag and participates in conditional execution and arithmetic. The carry flag is updated by arithmetic operations and can be used by conditional branches and comparisons, mirroring the role it plays in other modern designs. ARM’s approach emphasizes a unified set of condition codes and efficient handling of carry in both scalar and vector contexts. See ARM architecture for more on how the C flag integrates with status and conditional execution.
Other notable architectures - Many other RISC and CISC families expose a carry indicator in their own flavor of a status or condition code register, with variations in how carry is exposed to instructions and how it interacts with specific addressing modes or instruction encodings. Concepts such as carry-lookahead adders and carry-propagate structures are common in the hardware implementations that speed up carry generation and propagation in the arithmetic logic unit. See Carry-lookahead adder and Carry-propagate adder for design perspectives.
Examples and use cases
- Multi-precision addition: To add two 128-bit numbers stored as four 32-bit words, a processor adds the lowest words first, records the CF, and uses that CF as the input carry for the next pair of words. This pattern continues across all word boundaries, yielding a correct 128-bit result. The same pattern applies to subtraction and comparison in multi-word contexts.
- Conditional operations: After a comparison or arithmetic step, a programmer can branch based on the carry result (for unsigned comparisons) using the architecture’s conditional jump instructions. For example, a sequence might perform a test and then jump if a carry is present, enabling rapid decision-making in tight loops or numerical routines.
- In-place hardware support: Some algorithms leverage the CF to chain operations without additional temporary storage, improving throughput for element-wise operations on large datasets or in big-integer libraries. See big-integer arithmetic for higher-level implications.
See also