Avoid bit flags (& and |)

Use the Taligent-defined Boolean type if you want to keep Boolean flags. Unless saving space is important in a data structure or argument, the code required to interpret single-bit flags is larger and slower than the code to interpret the Boolean type.

NOTE In the future, Taligent might support bool, the new C++ built-in type for Booleans. For now, stick with the Taligent-defined type.

If you need to use sets of single-bit flags, don't use the & and | operators to test and set these flags because doing so is error prone. (More than one bug has arisen from & used instead of |). Instead, use C++'s (and C's) bit-field facility. This lets the compiler allocate the storage and generate the code for testing and setting. Also, the compiler can take advantage of special instructions not accessible via | and &.

The bit-wise operators are useful in cases where bits must be laid out carefully in storage to match some existing definition or hardware registers, rather than left up to the vagaries of the compiler. However, such code is usually not portable and should be isolated. For more information on isolating code, see "Nonportable code" on page 143.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker