Flag
In computer science, a flag is a value that acts as a signal for a function or process. The value of the flag is used to determine the next step of a program. Flags are often binary flags, which contain a boolean value (true or false). However, not all flags are binary, meaning they can store a range of values.
You can think of a binary flag as a small red flag that is laying flat when it is false, but pops up when it is true. A raised flag says to a program, “Stop – do something different.” A common example of a flag in computer programming is a variable in a while loop. The PHP loop below will iterate until $flag is set to true.
$flag = false;
$i = 1;
while (!$flag) // stop when $flag is true
{
echo “$i, “;
$i++; // increment $i
if ($i > 100) $flag = true;
}
The above code will print out numbers (1, 2, 3…) until 100. Then the loop will break because $flag will be set to true. Using a flag in this context is effective, but unnecessary. Instead, the while loop condition could have been while ($i < 101) instead of while (!$flag). This would produce the same result and eliminate the need for the $flag variable. Efficiently written programs rarely need explicit flags since an existing variable within a function can often be used as a flag.
A binary flag only requires one bit, which can be set to 0 or 1. However, bytes have eight bits, meaning seven bits are unused when a single byte stores a binary flag. While a single byte is still a very small amount of data, a programmer may choose to use a single byte to store multiple binary flags.
Non-Binary Flags
Non-binary flags use multiple bits and can store more than "yes or no" or "true or false." These types of flags require more than one bit, but not necessarily a full byte. For example, two bits can produce four possible options.
00 = option A
01 = option B
10 = option C
11 = option D
You can think of a non-binary flag as a flag with multiple colors. A program can check to see if 1) if the multi-bit flag is set and 2) what value it contains. Depending on the value (or "color") of the flag, the program will continue in the corresponding direction.
Updated May 17, 2018 by Per C.
APA
MLA
Chicago
HTML
Link
https://techterms.com/definition/flag
Copy