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.

  1. 00 = option A
  2. 01 = option B
  3. 10 = option C
  4. 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

Definitions by TechTerms.com

The definition of Flag on this page is an original TechTerms.com definition. If you would like to reference this page or cite this definition, you can use the green citation links above.

The goal of TechTerms.com is to explain computer terminology in a way that is easy to understand. We strive for simplicity and accuracy with every definition we publish. If you have feedback about the Flag definition or would like to suggest a new technical term, please contact us.

Want to learn more tech terms? Subscribe to the daily or weekly newsletter and get featured terms and quizzes delivered to your inbox.

Sign up for the free TechTerms Newsletter

How often would you like to receive an email?

You can unsubscribe or change your frequency setting at any time using the links available in each email.

Questions? Please contact us.