Fact-checked by Grok 2 weeks ago

Carry flag

The carry flag (often abbreviated as CF) is a single-bit flag in a processor's that signals whether an arithmetic operation, such as or , has produced a carry-out from the most significant bit (MSB) or required a borrow-in for the operation, primarily serving to detect in unsigned . This flag is essential for handling multi-precision , where it propagates carry or borrow values between successive operations to compute results larger than a single can hold. In the x86 architecture, the carry flag occupies bit 0 of the EFLAGS or RFLAGS register and is set to 1 upon a carry-out during instructions like ADD or , cleared to 0 otherwise, and can be explicitly manipulated using instructions such as STC (set), (clear), or (complement). It distinguishes itself from the (OF), which handles signed , enabling conditional jumps (e.g., JC for jump if carry) that support efficient unsigned comparisons and loop controls in programming. Similarly, in architectures, the carry flag in the application program (APSR) is set to 1 on addition if an unsigned overflow occurs or on if no borrow is needed, and it also captures the last bit shifted out in shift operations, facilitating conditional execution of instructions based on arithmetic outcomes. The carry flag's role extends to bit manipulation instructions like shifts (SHL, SHR) and rotates (ROL, ), where it stores bits to enable precise control in low-level operations, and it remains a foundational element in CPU design across architectures for ensuring arithmetic integrity without software intervention.

Fundamentals

Definition

The carry flag (CF), also known as the C flag in some architectures, is a single-bit status flag within a processor's that indicates a carry-out from the most significant bit (MSB) position during unsigned arithmetic operations, such as , or during certain instructions like shifts and rotates. In the x86 architecture, for instance, the carry flag occupies bit 0 of the EFLAGS register and is set to 1 when a carry occurs from the MSB or a borrow is required into the MSB for , while it is cleared to 0 otherwise. Similarly, in architectures, the carry flag is part of the application program status register (APSR) and reflects carry conditions from arithmetic instructions. Its primary purpose is to detect in unsigned operations, signaling when the result cannot be fully represented within the operand's bit width and requires an additional bit. For example, in an 8-bit unsigned , if the sum exceeds 255 (the maximum value representable in 8 bits), a carry-out into a hypothetical 9th bit sets the to 1, enabling software to handle the appropriately, such as in multi-precision . This mechanism is essential for maintaining accuracy in computations involving unsigned values across various processor designs. In terms of mechanics, for an n-bit of two operands, the carry flag is set to 1 if a carry propagates out of the MSB, indicating the sum necessitates an (n+1)th bit to represent it fully. Consider a 4-bit example: adding 10112 ( 11) and 01012 ( 5) produces 100002 ( 16), where the leading 1 beyond the 4 bits sets the carry flag to 1. This flag's behavior is consistent in its role for detecting such carry-outs, though its exact setting conditions may vary slightly by and .

Binary Representation

In x86 architecture, the carry flag () is encoded as the least significant bit, designated as bit 0, of the EFLAGS or RFLAGS register; bit positions vary by architecture. It is set to 1 to indicate the generation of a carry out from the most significant bit during an operation and cleared to 0 in the absence of such a carry. In x86-like architectures, dedicated instructions enable explicit manipulation of the carry flag, including STC (opcode F9) to set to 1, CLC (opcode F8) to clear to 0, and (opcode F5) to complement its current value by toggling between 0 and 1. The carry flag interacts with the , also known as the status word, through automatic updates following specific operations. For instance, arithmetic instructions such as ADD set or clear based on whether a carry out occurs from the most significant bit, whereas non-arithmetic instructions like leave unchanged. In operations, can be conceptually determined as follows: if the of the source and destination operands exceeds the maximum unsigned value for the operand size (2^n - 1 for n bits), then ← 1; otherwise, ← 0. Software visibility into the carry flag is provided via instructions that save or restore the entire , such as PUSHF to push it onto the and POPF to pop and restore it, or through direct testing in using conditional jumps like JC (jump if carry) or JNC (jump if no carry).

Arithmetic Applications

Addition Operations

In within computer processors, the carry flag is set to 1 if the sum of two n-bit unsigned operands exceeds $2^n - 1, indicating an for unsigned arithmetic that requires handling beyond the n-bit width. This occurs specifically when a carry-out is generated from the most significant bit (MSB) during the . The mechanics of carry generation rely on a ripple-carry structure, where propagates from the least significant bit (LSB) to the MSB. Each bit position employs a full that takes two bits and an incoming carry-in, producing a sum bit and a carry-out to the next position. The carry ripples sequentially: if the sum at any bit exceeds 1 (considering the carry-in), it sets the carry-out to 1, potentially propagating through higher bits until reaching the MSB. The carry-out from the MSB, which sets the carry flag, follows the full adder logic: CF = (A_{n-1} \land B_{n-1}) \lor ((A_{n-1} \oplus B_{n-1}) \land C_{n-1}) where A_{n-1} and B_{n-1} are the MSBs of the operands, and C_{n-1} is the carry-in to the MSB. For instance, in an 8-bit addition of 255 ($11111111_2) and 1 ($00000001_2), the result is 256, which modulo $2^8 yields $00000000_2 with a carry-out of 1 from the MSB, thus setting the carry flag. This flag enables efficient multi-byte addition in unsigned arithmetic, as seen in instructions like (Add with Carry), which adds the operands plus the current carry flag value as carry-in to the LSB, facilitating chained operations for larger integers across multiple registers or memory words.

Subtraction and Multi-Precision

In subtraction operations within computer architectures, the carry flag () functions as a borrow indicator for unsigned arithmetic. Specifically, when performing a subtraction using the SUB instruction, is set to 1 if the subtrahend (source operand) is greater than the minuend (destination operand), indicating that a borrow was required from a higher significance bit; otherwise, it is cleared to 0. This behavior arises because subtraction in representation is implemented as the addition of the two's complement negation of the subtrahend, where the borrow corresponds to a carry out from the most significant bit during this addition. For example, consider an 8-bit unsigned subtraction of 10 (binary 00001010) from 5 (binary 00000101). The operation yields a result of 11111011 (decimal 251 in unsigned interpretation), and CF is set to 1 because 5 < 10, requiring a borrow. In general, for unsigned operands A and B, CF = 1 if A < B after subtraction, which is logically equivalent to the negation of (A ≥ B). The carry flag also plays a crucial role in multi-precision arithmetic, where it propagates borrows across multiple limbs (fixed-width portions of larger operands) to enable operations on numbers exceeding the processor's native word size. This is typically achieved using the SBB (Subtract with Borrow) instruction, which subtracts the source operand plus the current value of CF from the destination: DEST ← DEST - (SRC + CF), setting CF based on whether an additional borrow is needed. For instance, to subtract two 64-bit unsigned integers on a 32-bit architecture, the least significant 32 bits are subtracted first with SUB (setting CF if a borrow occurs), followed by SBB on the most significant 32 bits to incorporate and propagate the borrow from the lower limb. This chaining mechanism ensures accurate handling of large integers without native support for wider operands.

Architectural Implementations

x86 Family

In the x86 architecture, the carry flag (CF) resides at bit 0 of the in 16-bit mode, the EFLAGS register in 32-bit mode, and the RFLAGS register in 64-bit mode. This positioning allows CF to indicate carry-out from the most significant bit during arithmetic operations or serve as an input for multi-precision computations across all operating modes, including real, protected, virtual-8086, and long (64-bit). Key instructions that update CF include ADD, which sets the flag if there is a carry-out from the most significant bit of the result (indicating unsigned ), and , which adds the source , destination , and the current CF value, setting CF if a carry-out occurs from the most significant bit. Similarly, sets CF if the destination is smaller than the source for unsigned operands (indicating a borrow), while SBB subtracts the source and the current CF from the destination, setting CF if a borrow is required. Rotation instructions such as (rotate left through carry) and RCR (rotate right through carry) treat CF as an extension of the operand, setting it to the bit shifted out of the most significant position or the previous CF value, depending on the rotation count. Conditional jumps based on CF include JC (jump if carry), which branches if CF is 1, and JNC (jump if no carry), which branches if CF is 0; these are commonly used in loops for multi-precision arithmetic, such as adding or subtracting numbers larger than the register size by propagating the carry or borrow across iterations. The carry flag mechanism was introduced with the microprocessor, released on June 8, 1978, as part of its 16-bit to support unsigned arithmetic and . In modern implementations, CF behavior remains consistent and size-specific: for example, a 32-bit ADD operation in 64-bit mode sets CF based on carry-out from bit 31 of the lower 32 bits, unaffected by the upper 32 bits of the 64-bit register, ensuring compatibility while allowing operand-size overrides via prefixes.

Other Instruction Sets

In the ARM architecture, the carry flag is implemented as the C bit (bit 29) in the Application Program Status Register (APSR), which is set to 1 when an instruction results in a carry condition, such as unsigned overflow during addition. This flag supports instructions like ADDS, which performs addition and updates the flags including C based on the result, and ADCS, which adds two operands along with the existing C flag value while setting the flags for the subsequent operation. RISC-V's base integer set lacks a dedicated carry flag, relying instead on separate to detect and propagate carry for multi-precision . Carry detection typically uses the SLTU (set less than unsigned) to check if the sum of two exceeds the first operand, effectively identifying as a carry out. Extensions such as Zba ( for generation) provide instructions like add.uw that can assist in certain operations but do not introduce a persistent ; add-with-carry operations are handled implicitly through sequencing multiple or / registers (CSRs) in extended configurations. As of 2025, proposals for dedicated carry handling extensions, such as for multi-word , remain under development but have not been ratified. The MIPS architecture omits a dedicated carry flag entirely, simulating carry handling through temporary registers and comparison instructions for multi-word operations. Specifically, the SLTU instruction sets a register to 1 if the first operand is less than the second under unsigned interpretation, enabling carry detection in addition (e.g., by comparing the sum against one operand) and borrow simulation in subtraction without altering a global flag state. Variations across other instruction sets highlight design trade-offs in flag management. In PowerPC, the carry (CA) bit resides in the XER special-purpose register and is updated by arithmetic instructions like ADD and SUBF to indicate unsigned overflow or borrow, while the summary overflow (SO) bit in CR0 (condition register field 0) latches overflow conditions persistently until cleared. Some RISC architectures merge carry and borrow semantics into a single bit for simplicity, as seen in PowerPC's unified handling, whereas others like and base omit flags altogether to reduce hardware complexity and favor explicit register-based propagation. Portability challenges arise in flag-less architectures, where software must emulate carry propagation using additional instructions, often resulting in higher instruction counts and performance overhead compared to flag-supported designs. This emulation is essential for applications requiring big-integer operations.

Versus Borrow Flag

In many processor architectures, the carry flag serves a dual role in arithmetic operations, functioning as a borrow indicator during to facilitate multi-precision computations. For instance, in the x86 architecture, the carry flag () is set to 1 when a () requires a borrow from a higher significant bit position, meaning the unsigned value of the destination is less than that of the source . This same flag is then used as an input for the subtract with borrow (SBB) instruction, which subtracts the source plus the value (1 for borrow pending, 0 otherwise) from the destination, enabling chained multi-word subtractions. This duality contrasts with architectures that employ a dedicated extend or borrow flag separate from the primary carry flag, though such designs are relatively uncommon due to the added hardware overhead of maintaining additional status bits. In the family, for example, the carry flag (C) indicates borrow during single-precision subtractions (e.g., , CMP), set to 1 if a borrow occurs into the most significant bit. However, a distinct extend flag (X) is provided for multi-precision operations, mirroring C in most arithmetic instructions but persisting independently to propagate borrow across multiple words in instructions like SUBX (subtract extended). Not all instructions update both flags equally; for instance, certain shifts or logical operations may affect C without altering X, allowing finer control in borrow-only scenarios where no carry-in from prior additions is involved. The polarity of the borrow indication can also vary, introducing conceptual overlap but operational differences. In architectures, the carry flag (C) is cleared (0) when a subtraction generates a borrow, effectively inverting the x86 convention where CF=1 signals borrow; instead, C=1 denotes no borrow, optimizing condition code usage for conditional execution. Dedicated borrow flags remain rare outside specialized designs like certain digital signal processors (DSPs), where separate flags may reduce logic complexity for tasks but increase register size—trading simplicity for precision in borrow propagation.

Versus Overflow Flag

The carry flag (CF) and overflow flag (OF) provide distinct mechanisms for detecting arithmetic overflow in binary operations, tailored to different number interpretations. The CF signals unsigned overflow by capturing a carry out from the most significant bit (MSB) during , indicating that the result has wrapped around modulo $2^n for an n-bit , without regard to sign. In contrast, the OF detects signed overflow in representation, flagging inconsistencies where the of the result does not align with the expected outcome for signed operands—specifically, when two positive numbers sum to a negative result or two negative numbers sum to a positive one. For addition, the OF is computed as the exclusive OR of the carry into the sign bit and the carry out from the sign bit: OF = C_{in}^{n-1} \oplus C_{out}^{n-1} where C_{in}^{n-1} is the carry generated into the MSB position from the lower bits, and C_{out}^{n-1} is the carry generated out of the MSB. This formulation identifies signed overflow precisely because it highlights a discrepancy in carry propagation at the sign position, which would not occur in valid signed arithmetic. The CF, however, is simply set to C_{out}^{n-1}, ignoring any sign-specific carry analysis. In an 8-bit signed addition, +127 ($01111111_2) + 1 ($00000001_2) produces $10000000_2 (-128), with no carry out (CF = 0) but a carry into the differing from the carry out (OF = 1), confirming signed as the positive sum exceeds the maximum representable value of +127. For unsigned interpretation of the same bits, 255 ($11111111_2) + 1 ($00000001_2) yields $00000000_2 with carry out (CF = 1), but the carries into and out of the match (OF = 0), as the signed values -1 + 1 = 0 stay within bounds. The CF supports unsigned use cases like validating loop iterations or detecting modular wraparound in counters, where exceeding $2^n - 1 requires handling the modulo behavior. The OF is critical for signed validations, such as in algebraic computations or data processing, to prevent erroneous sign changes that could lead to incorrect results. Conditional instructions leverage both, with CF enabling jumps for unsigned conditions and OF for signed overflow checks, like JO (jump if overflow).

References

  1. [1]
    [PDF] Intel® 64 and IA-32 Architectures Software Developer's Manual
    Jan 2, 2012 · NOTE: The Intel® 64 and IA-32 Architectures Software Developer's Manual consists of nine volumes: Basic Architecture, Order Number 253665; ...
  2. [2]
    Carry flag - Arm Developer
    The carry (C) flag is set when an operation results in a carry, or when a subtraction results in no borrow.Missing: definition | Show results with:definition
  3. [3]
    [PDF] Intel® 64 and IA-32 Architectures Software Developer's Manual
    NOTE: The Intel® 64 and IA-32 Architectures Software Developer's Manual consists of nine volumes: Basic Architecture, Order Number 253665 ... Carry Flag ...
  4. [4]
  5. [5]
    The CARRY flag and OVERFLOW flag in binary arithmetic
    1. The carry flag is set if the addition of two numbers causes a carry out of the most significant (leftmost) bits added. 1111 + 0001 = 0000 ...
  6. [6]
    [PDF] 6. Using flags to monitor the outcome of arithmetic operations
    For ease of reference, the purposes of the flags which are most important for simple programming tasks are summarized in Table 6.1. Flag. Carry Flag.Missing: science | Show results with:science
  7. [7]
    [PDF] CS 140 Lecture 6 - UCSD CSE
    Full Adder Composed of Half Adders. HA. HA a b c in x sum c out. OR sum c out c ... are both 1. G i. = A i. B i. • A column will propagate a carry in to the carry ...
  8. [8]
    [PDF] 1-bit Full Adder - Computer Science (CS)
    We can then express the general carry bit formula: The formula indicates that if g i. = 1 then the adder generates a carry-out no matter what the value of ...
  9. [9]
    [PDF] Intel® 64 and IA-32 Architectures Software Developer's Manual
    NOTE: The Intel 64 and IA-32 Architectures Software Developer's Manual consists of four volumes: Basic Architecture, Order Number 253665 ... Carry Flag ...
  10. [10]
    Two's Complement - Cornell: Computer Science
    To get the two's complement negative notation of an integer, you write out the number in binary. You then invert the digits, and add one to the result.Contents and Introduction · Conversion from Two's... · Conversion to Two's...
  11. [11]
    [PDF] CSC 370 – Computer Architecture and Organization
    CSC 370 – Computer Architecture and Organization. Lecture 9 – IA-32 ... – Carry Flag (CF) - set when the result of unsigned arithmetic is too large ...
  12. [12]
    The Intel ® 8086 and the IBM PC
    Intel introduced the 8086 microprocessor in 1978. Completed in just 18 months, the 8086 was a game changer for several reasons: its design allowed for much more ...
  13. [13]
    [PDF] 40 years of X86 by Marcus Yam 2018 - Steve Morse
    Jan 18, 2018 · Originally released on June 8, 1978, the 8086 was the start of x86 architecture, the foundation for processor design through today-including ...
  14. [14]
    The Application Program Status Register (APSR) - Arm Developer
    A result of zero often indicates an equal result from a comparison. C, bit[29]. Carry condition flag. Set to 1 if the instruction results in a carry condition, ...
  15. [15]
    CPSR: Current Program Status Register - Arm Developer
    C, bit [29]. Carry condition flag. Set to 1 if the last flag-setting instruction resulted in a carry condition, for example an unsigned overflow on an ...
  16. [16]
    ADCS - Arm A-profile A64 Instruction Set Architecture
    This instruction adds two register values and the Carry flag value, and writes the result to the destination register. It updates the condition flags based on ...Missing: APSR | Show results with:APSR
  17. [17]
    [PDF] Towards a RISC-V Instruction Set Extension for Multi-word Arithmetic
    RISC-V does not support an explicit carry flag, so programmers must add another instruction detecting if the result overflowed to implement multi-word ...
  18. [18]
    [PDF] Fundamentals of Computer Systems - The MIPS Instruction Set
    Set on less than unsigned andi. AND immediate ori. OR immediate xori. Exclusive ... zero, carry); conditional branches test these flags. Registers that are ...Missing: flag | Show results with:flag
  19. [19]
    The PowerPC 600 series, part 3: Arithmetic - The Old New Thing
    Aug 8, 2018 · The PowerPC uses true carry for both addition and subtraction. This is different from the x86 family of processors, for which the carry flag is ...
  20. [20]
    SBB — Integer Subtraction With Borrow
    The state of the CF flag represents a borrow from a previous subtraction. When an immediate value is used as an operand, it is sign-extended to the length ...
  21. [21]
    [PDF] Motorola M68000 Family Programmer's Reference Manual
    This manual contains detailed information about software instructions used by the microprocessors and coprocessors in the M68000 family, including: MC68000.
  22. [22]
    [PDF] ADSP-219x/2192 DSP Hardware Reference - Analog Devices
    ... architecture Digital. Signal Processor (DSP) core and ADSP-2192 DSP product. The architec- tural descriptions cover functional blocks, buses, and ports ...