Flag (computer science)
The English term flag is related to German flag , but should never be translated that way. In the English language, an expanded definition of the term has emerged, in which to flag not only means showing the flag, setting it, pulling it up, but more generally means signaling and flagging ( to flag down a taxi ). This probably goes back to the custom in the rural areas of the USA to put a small (red) flag on the mailbox at the property entrance (remote from the house), which was raised when new mail was placed (incoming or outgoing). Instead of the more complicated term binary semaphore for such signaling devices, the term flag spread - the flag shape and designation as (binary) semaphore goes back to optical telegraphy and the winker alphabet .
- Often a Boolean variable is used to represent a flag. However, you can calculate with variables, and a Boolean variable can contain the value "false". A flag, however, knows the states “is set” or “is not set” (in the sense of “is not there at all”). A Boolean variable, on the other hand, is definitely there / declared, even if it is set to "false".
- If more than two states have to be described, an enumeration type is used.
- If only the first setting of a flag is relevant (it should not be possible to delete it later), a flag is similar to a symbol .
Flags in the processor
Flags in programming
Which data format is used to represent a flag depends on the implementation: A flag can be implemented in many programming languages using a Boolean variable. Usually the binary values 0 and 1 or variants that are more understandable for humans such as "set" and "not set" are used.
If more than two states are to be displayed, this is usually not referred to as a “flag”, but as a status variable / constant . From the English language, the term (binary) semaphore or mutex locks can also be found in programming for synchronizing status variables .
Sometimes flags are also used for the conditional compilation of source code; This is very common, especially in the C and C ++ languages:
#define COMPILE_FLAG // ... #ifdef COMPILE_FLAG // Code-Block 1, der ins Programm einfliesst, wenn COMPILE_FLAG gefunden wird. #else // Code-Block 2, der stattdessen einfliesst, wenn COMPILE_FLAG _nicht_ gefunden wird. #endif
Flags in data transmission
When transmitting data, it is sometimes necessary to signal certain states or signal properties. Instead of separate lines, certain bit patterns can be used, which are then "status flags". The data preamble is particularly widespread in order to signal the start of a transmission and to enable the receiver to synchronize, as well as the start and stop bit .
Through suitable coding of the data to be transmitted, there can be bit patterns that cannot appear in the useful data stream. Instead of error detection / correction, these can be used as control signals.