information about the processor's current state. As an example, the status register of the 6502 processor is shown below: These bits are set by the processor following the result of an operation. Certain bits (such as the Carry, Interrupt-disable, and Decimal flags) may be explicitly controlled using set and clear instructions. Additionally, branching instructions are also defined to alter execution based on the current state of a flag. For an instance, after an ADC (Add with Carry) instruction, the BVS (Branch on oVerflow Set) instruction may be used to jump based on whether the overflow flag was set by the processor following the result of the addition instruction. Extracting bits from flag words A subset of flags in a flag field may be extracted by ANDing with a mask. A large number of languages support the shift operator (<<) where 1 << n aligns a single bit to the nth position. Most also support the use of the AND operator (&) to isolate the value of one or more bits. If the status-byte from a device is 0x67 and the 5th flag bit indicates data-ready. The mask-byte is . ANDing the status-byte 0x67 (0110 0111 in binary) with the mask-byte 0x20(0010 0000 in binary) evaluates to 0x20. This means the flag bit is set i.e the device has data-ready. If the flag-bit had not been set, this would have evaluated to 0 i.e. there is no data available from the device. To check the nth bit from a variable v, perform the operation: bool nth_is_set = (v & (1 << n)) != 0; bool nth_is_set = (v >> n) & 1; Changing bits in flag words Writing, reading or toggling bits in flags can be done only using the OR, AND and NOT operations – operations which can be performed quickly in the processor. To set a bit, OR the status byte with a mask byte. Any bits set in the mask byte or the status byte will be set in the result. To toggle a bit, XOR the status byte and the mask byte. This will set a bit if it is cleared or clear a bit if it is set. See also Binary code Bitboard, used in chess and similar games Bit array (or bit string) Word (computer architecture) Mask (computing) Program status word Status register FLAGS register (computing) Control register References External links Explanation from a book