Jump to content

Carry flag: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m indef article inserted
Mention the carry flag as an input earlier on, and provide an example
Line 1: Line 1:
In [[computer]] [[processors]] '''carry flag''' is a single bit in a system status (flag) register used to indicate when an arithmetic [[carry]] or borrow has been generated out of the most significant [[Arithmetic logic unit|ALU]] bit position. In some machines such as the [[MOS Technology 6502]], after a subtract operation the carry flag indicates the absence of a borrow.
In [[computer]] [[processors]] '''carry flag''' is a single bit in a system status (flag) register used to indicate when an arithmetic [[carry]] or borrow has been generated out of the most significant [[Arithmetic logic unit|ALU]] bit position. In some machines such as the [[MOS Technology 6502]], after a subtract operation the carry flag indicates the absence of a borrow.

The carry flag can also be used as an extra operand to certain instructions, e.g. a subtract with carry instruction, a shift instruction, or a rotate through carry instruction. In these insructions, the carry is an input (i.e. the result depends on the value of the carry flag before the instruction) and an output (i.e. the carry flag is changed by the instruction). The use of the carry flag in this manner enables multi-word add, subtract, shift, or rotate operations.


An example of the use of the carry flag is what happens if you were to add 255 and 255 using 8-bit unsigned integers. The mathematical answer is 510, but in binary this is 1 1111 1110 which requires nine bits. The 8-bit result is 1111 1110, or 254. Since there is carry out of bit 7, the carry flag is set to 1, indicating that the result is invalid in an 8-bit unsigned interpretation. Alternatively, the valid 9-bit result is the concatenation of the carry flag with the result. Note that in an 8-bit two's complement interpretation, the operation is -1 + -1 and yields the correct result of -2, with no [[overflow flag|overflow]].
An example of the use of the carry flag is what happens if you were to add 255 and 255 using 8-bit unsigned integers. The mathematical answer is 510, but in binary this is 1 1111 1110 which requires nine bits. The 8-bit result is 1111 1110, or 254. Since there is carry out of bit 7, the carry flag is set to 1, indicating that the result is invalid in an 8-bit unsigned interpretation. Alternatively, the valid 9-bit result is the concatenation of the carry flag with the result. Note that in an 8-bit two's complement interpretation, the operation is -1 + -1 and yields the correct result of -2, with no [[overflow flag|overflow]].

As an example of the carry flag used as input, consider an 8-bit register with the bit pattern 0101 0101, with the carry flag set (value is 1) before a rotate left through carry instruction. The result is 1010 1011, and the carry flag is cleared because the most significant bit (a zero) was rotated into the carry bit.


Carry flag is changed in the [[x86]] processor family by the following instructions (referring to 80386 Intel manual):
Carry flag is changed in the [[x86]] processor family by the following instructions (referring to 80386 Intel manual):
Line 10: Line 14:
* TEST (equivalent to AND without storing the result).
* TEST (equivalent to AND without storing the result).


The carry flag could be considered to be the unsigned equivalent of the [[overflow flag]].
The carry flag after an arithmetic operation could be considered to be the unsigned equivalent of the [[overflow flag]].

The carry flag may be used to add numbers with more bits than processor can handle normally, for example adding two 128-bit values, using the special processor instruction '''add with carry (ADC)'''. Subtract and shift instructions can be extended in similar ways.





Revision as of 22:48, 3 October 2006

In computer processors carry flag is a single bit in a system status (flag) register used to indicate when an arithmetic carry or borrow has been generated out of the most significant ALU bit position. In some machines such as the MOS Technology 6502, after a subtract operation the carry flag indicates the absence of a borrow.

The carry flag can also be used as an extra operand to certain instructions, e.g. a subtract with carry instruction, a shift instruction, or a rotate through carry instruction. In these insructions, the carry is an input (i.e. the result depends on the value of the carry flag before the instruction) and an output (i.e. the carry flag is changed by the instruction). The use of the carry flag in this manner enables multi-word add, subtract, shift, or rotate operations.

An example of the use of the carry flag is what happens if you were to add 255 and 255 using 8-bit unsigned integers. The mathematical answer is 510, but in binary this is 1 1111 1110 which requires nine bits. The 8-bit result is 1111 1110, or 254. Since there is carry out of bit 7, the carry flag is set to 1, indicating that the result is invalid in an 8-bit unsigned interpretation. Alternatively, the valid 9-bit result is the concatenation of the carry flag with the result. Note that in an 8-bit two's complement interpretation, the operation is -1 + -1 and yields the correct result of -2, with no overflow.

As an example of the carry flag used as input, consider an 8-bit register with the bit pattern 0101 0101, with the carry flag set (value is 1) before a rotate left through carry instruction. The result is 1010 1011, and the carry flag is cleared because the most significant bit (a zero) was rotated into the carry bit.

Carry flag is changed in the x86 processor family by the following instructions (referring to 80386 Intel manual):

  • All arithmetic operations;
  • Compare instructions (equivalent to a subtract without storing the result).

The carry flag is cleared in the x86 processor family by the following instructions:

  • Logical operations - XOR, AND, OR
  • TEST (equivalent to AND without storing the result).

The carry flag after an arithmetic operation could be considered to be the unsigned equivalent of the overflow flag.