What Is a Flag?
In programming, a flag is a variable used to represent a true or false value.
Flags are useful for test statements such as IF statements. Flags represent
a state within your algorithm (or program) that are tested to make decisions
based on their value. An example of a flag can be whether or not it is
daytime or nighttime if you have an computerized house which controls your
lighting system.
if(time >= 1800) then
NIGHT = true
else
NIGHT = false
if(NIGHT) then
turn ON light
else
turn OFF light
In the above example the flag NIGHT is false during the daytime and true
during the nighttime. We used true and false as the values but you can
also use 1 and 0 respectively, almost all languages will treat them as
equivalents.
Also, in the above example, the flag was basically useless since we could
have turned the lights on or off based on the time, but we used the time
to set the flag and the state of the flag to control the lights. This was
only for an example. When using functions, you will sometimes need to set
flags with global scope so that other functions not called by an immediate
function can assess the state of the algorithm.
Advanced flags
To understand the following, you must understand the binary numbering system
and 8 bits (a byte) adds up into a decimal number between 0 and 255. Since
a byte has 8 bits which can be either true or false, or 1 or 0 respectively,
you can use a single integer to represent up to 8 different flags. This
concept is for advanced users who want to make their program consume the
least amount of RAM as possible. An example where this type of flag
programming occurs is in integrated electronics from microwaves (is the
door closed) to automobiles (windshieldwipers = true, then headlights on).
The following is an example of a flag where each bit will represent a
boolean value to yield 8 different flags but only consume the amount of RAM
required by a single integer. We will also see how code can be optimized to
speed up an algorithm.
int CAR = 0
The integer representing 8 different flags has been created.
All flags are also set to 0 since 0 in decimal is also all
0s in binary.
0 0 0 0 0 0 0 0
| | | | | | | |--> Windshield wipers off
| | | | | | |----> headlights off
| | | | | |------> seatbelt off
| | | | |--------> all doors closed
| | | |----------> engine not running
| | |------------> alarm system not armed
| |--------------> air bags off
|----------------> brake pedal not applied (tailights)
Now, CAR = 32. Examing the following pattern:
0 0 1 0 0 0 0 0
This means the alarm system is armed all all other systems are false.
The number looks like a binary number (a byte). In decimal notation,
the byte would equal:
128 64 32 16 8 4 2 1
0 0 1 0 0 0 0 0 = 32
The conclusion is that if the CAR variable is 32, all systems are off
except for the alarm system. It is most likely that when the alarm
is armed, all doors will also be closed, this means that the 8 in
decimal bit will also be true, thus giving the following:
128 64 32 16 8 4 2 1
0 0 1 0 1 0 0 0 = 40
It follows that when the car is parked, the CAR variable in the
integrated electronic car controller will be equal to 40: all
doors are closed and the car alarm is armed. If the CAR variable
all the sudden was equal to 32, that means the system is armed
but a car door is open... sound the alarm! The car is being stolen.
On closer inspection, we see that the test statement that would
have sounded the alarm only had to make ONE test if(CAR != 40).
In the conventional use of flags, it would have had to make TWO
tests: is the alarm armed AND is a door open. We reduced the
comparisons, thus making a faster algorithm.
While reducing logical comparison statements and the amount of RAM consumed
by your application using advanced flags, you may think, how do I change a
bit in the integer. It is rather simple. The bits exists in two ways, both
in reality and in theory. The computer will hold all decimals in binary
notation because that is how they work, but you using the decimals as
flags is only theoretical in application. You do not have to change the
bits using strange bitwise operators. You can simply add and subtract
decimals from the current CAR flag.
To show that the car alarm is armed, add 32, to show that all car doors are
closed, add 8. If a car door is opened, subtract 8. This will effectively
change the bits both in reality (in the hardware) and logically (the
algorithm).