A Boolean flag, truth bit or truth flag in
computer science is a
Boolean value
In mathematics and mathematical logic, Boolean algebra is a branch of algebra. It differs from elementary algebra in two ways. First, the values of the variables are the truth values ''true'' and ''false'', usually denoted 1 and 0, whereas in e ...
represented as one or more bits, which encodes a state variable with two possible values.
Memory usage
A single
byte can contain up to 8 separate Boolean flags by mapping one Boolean flag to each bit, making it a very economical and dense method of data storage. This is known as a packed representation or bit-packing, and the opposite encoding with only one Boolean flag per byte used is known as a sparse representation. For
byte-addressable memory the packed representation requires a
bit mask
In computer science, a mask or bitmask is data that is used for bitwise operations, particularly in a bit field. Using a mask, multiple bits in a byte, nibble, Word (computer architecture), word, etc. can be set either on or off, or inverted fro ...
and
bit-shift
In computer programming, a bitwise operation operates on a bit string, a bit array or a Binary numeral system, binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-l ...
to access individual flags in each byte, which can require additional instructions, whereas the sparse representation requires no bit masking. Packed representations are more commonly found in hardware and
processor registers as
bit fields whereas sparse representations are more commonly found in software as
variables of one or more bytes in width, although packed representations can also be supported.
Efficiency
Most
computer languages support the setting and testing of single or multiple bits in combination for use as truth indicators and usually up to 256 different combinations of conditions can be tested for with just a single instruction on one byte using
bitwise operations
In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operati ...
. Advancements in
processor design and
parallel computing
Parallel computing is a type of computation in which many calculations or processes are carried out simultaneously. Large problems can often be divided into smaller ones, which can then be solved at the same time. There are several different fo ...
mean even more
Boolean algebra operations on Boolean flags can be done with just a single instruction using
SIMD technology, often implemented in programming languages as
compiler intrinsic functions.
Usage
Sometimes, programs are written to simply set flags when certain conditions are detected, rather than have multiple nested
conditional statements (e.g.
if
s) that can get quite complex. When all the conditions are tested for and all flags set on or off appropriately,
testing
An examination (exam or evaluation) or test is an educational assessment intended to measure a test-taker's knowledge, skill, aptitude, physical fitness, or classification in many other topics (e.g., beliefs). A test may be administered verba ...
can commence on various combinations of conditions - by reference to the flags instead of the variables themselves. This can simplify processing considerably and allows
decision table
Decision tables are a concise visual representation for specifying which actions to perform depending on given conditions. They are algorithms whose output is a set of actions. The information expressed in decision tables could also be represented ...
s to be implemented by mapping to their binary representations in memory.
See also
*
Boolean function
References
{{Reflist
Logic in computer science
Computer programming
Conditional constructs