What is the most efficient way to store flag values?
A flag is a value used to make a decision between two or more options in the execution of a program. For
instance, the /w flag on the MS-DOS dir command causes the command to display filenames in several
columns across the screen instead of displaying them one per line. In which a flag is used to indicate which of two possible types is held in a union.
Because a flag has a small number of values (often only two), it is tempting to save memory space by not
storing each flag in its own int or char.
Efficiency in this case is a tradeoff between size and speed. The most memory-space efficient way to store
a flag value is as single bits or groups of bits just large enough to hold all the possible values. This is because
most computers cannot address individual bits in memory, so the bit or bits of interest must be extracted from
the bytes that contain it.
The most time-efficient way to store flag values is to keep each in its own integer variable. Unfortunately,
this method can waste up to 31 bits of a 32-bit variable, which can lead to very inefficient use of memory.
If there are only a few flags, it doesn't matter how they are stored. If there are many flags, it might be
advantageous to store them packed in an array of characters or integers. They must then be extracted by a
process called bit masking, in which unwanted bits are removed from the ones of interest.
Sometimes it is possible to combine a flag with another value to save space. It might be possible to use high-
order bits of integers that have values smaller than what an integer can hold. Another possibility is that some
data is always a multiple of 2 or 4, so the low-order bits can be used to store a flag.
Bit fields are not portable. Because bit fields cannot span machine words, and because the number of bits
in a machine word is different on different machines, a particular program using bit fields might not even
compile on a particular machine.
Assuming that your program does compile, the order in which bits are assigned to bit fields is not defined.
Therefore, different compilers, or even different versions of the same compiler, could produce code that
would not work properly on data generated by compiled older code. Stay away from using bit fields, except
in cases in which the machine can directly address bits in memory and the compiler can generate code to take
advantage of it and the increase in speed to be gained would be essential to the operation of the program.
Is it better to bitshift a value than to multiply by 2?
Any decent optimizing compiler will generate the same code no matter which way you write it. Use whichever
form is more readable in the context in which it appears. The following program's assembler code can be
viewed with a tool such as CODEVIEW on DOS/Windows or the disassembler (usually called "dis") on
Example: Multiplying by 2 and shifting left by 1 are often the same.
We generally write numbers from left to right, with the most significant digit first. To understand what is
meant by the "significance" of a digit, think of how much happier you would be if the first digit of your
paycheck was increased by one compared to the last digit being increased by one.
The bits in a byte of computer memory can be considered digits of a number written in base 2. That means
the least significant bit represents one, the next bit represents 2�1, or 2, the next bit represents 2�2�1, or
4, and so on. If you consider two bytes of memory as representing a single 16-bit number, one byte will hold
the least significant 8 bits, and the other will hold the most significant 8 bits. Figure shows the bits
arranged into two bytes. The byte holding the least significant 8 bits is called the least significant byte, or
low-order byte. The byte containing the most significant 8 bits is the most significant byte, or high-
A 16-bit number takes two bytes of storage, a most significant byte and a least significant byte. If you write the 16-bit number on paper, you would start with the most significant byte and end with the least significant byte. There is no convention for which order to store
them in memory, however.
Let's call the most significant byte M and the least significant byte L. There are two possible ways to store
these bytes in memory. You could store M first, followed by L, or L first, followed by M. Storing byte M first
in memory is called "forward" or "big-endian" byte ordering. The term big endian comes from the fact that
the "big end" of the number comes first, and it is also a reference to the book Gulliver's Travels, in which the
term refers to people who eat their boiled eggs with the big end on top.
Storing byte L first is called "reverse" or "little-endian" byte ordering. Most machines store data in a big-
endian format. Intel CPUs store data in a little-endian format, however, which can be confusing when
someone is trying to connect an Intel microprocessor-based machine to anything else.
A 32-bit number takes four bytes of storage. Let's call them Mm, Ml, Lm, and Ll in decreasing order of
significance. There are 4! (4 factorial, or 24) different ways in which these bytes can be ordered. Over the
years, computer designers have used just about all 24 ways. The most popular two ways in use today, however,
are (Mm, Ml, Lm, Ll), which is big-endian, and (Ll, Lm, Ml, Mm), which is little-endian. As with 16-bit numbers,
most machines store 32-bit numbers in a big-endian format, but Intel machines store 32-bit numbers in a