# Flag field

(Redirected from Flag (computing))

In computer programming, a flag field is an integer interpreted as a sequence of boolean bits, each called a "flag".

Flags are typically found as members of a defined data structure, such as a database record, and the meaning of the value contained in a flag will generally be defined in relation to the data structure it is part of. In many cases, the binary value of a flag will be understood to represent one of several possible states or statuses. In other cases, the binary values may represent one or more attributes in a bit field, often related to abilities or permissions, such as "can be written to" or "can be deleted". However, there are many other possible meanings that can be assigned to flag values. One common use of flags is to mark or designate data structures for future processing.

Within microprocessors and other logic devices, flags are commonly used to control or indicate the intermediate or final state or outcome of different operations. Microprocessors typically have, for example, a status register that is composed of such flags, and the flags are used to indicate various post-operation conditions, such as when there has been an arithmetic overflow. The flags can be utilized in subsequent operations, such as in processing conditional jump instructions. For example a je (Jump if Equal) instruction in the X86 assembly language will result in a jump if the Z (zero) flag was set by some previous operation.

A command line switch is also known as a flag. Command line programs often start with an option parser that translates command line switches into flags in the sense of this article.

## Examples

### Processor status register

Taking the example of a 6502 processor's status register, the following information was held within 8 bits:

• Bit 7. Negative flag
• Bit 6. Overflow flag
• Bit 5. Unused
• Bit 4. Break flag
• Bit 3. Decimal flag
• Bit 2. Interrupt-disable flag
• Bit 1. Carry flag
• Bit 0. Zero flag

### Unix process exit code

A more general example would be the use of a Unix exit status as a flag word. In this case, the exit code is by the programmer to pass status information to another process. An imaginary program which returns the status of 8 burglar alarm switches connected to the printer port could set each of the bits in the exit code in turn, depending on whether the switches are closed or open.

## Extracting bits from flag words

A subset of flags in a flag field may be extracted by ANDing with a mask.

Suppose that status byte 103 (decimal) is returned, and that we want to check flag bit 5.

The flag we want to read is number 5 (counting from zero) - so the mask byte will be $2^5 = 32$. ANDing 32 with 103 gives 32, which means the flag bit is set. If the flag bit was not set, the result would have been 0.

In modern computing, the shift operator (<<) can be used to quickly perform the power-of-two. In general, a mask with the Nth bit set can be computed as

(1 << n)


Thus to check the Nth bit from a variable v, we can perform the operation

bool nth_is_set = (v & (1 << n)) != 0


## 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.

 int setBit(int val, int bit_position)
{
return val | (1 << bit_position);
}


To clear a bit, perform a NOT operation on the mask byte, then AND it with the status byte. The result will have the appropriate flag cleared (set to 0).

 int clearBit(int val, int bit_position)
{
return val & ~(1 << bit_position);
}


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.

 int toggleBit(int val, int bit_position)
{
return val ^ (1 << bit_position);
}
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 1, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.26389890909194946, "perplexity": 2072.329434796382}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-41/segments/1412037663417.12/warc/CC-MAIN-20140930004103-00268-ip-10-234-18-248.ec2.internal.warc.gz"}