- Bit field
A bit field is a common idiom used in
computer programming to store a set ofBoolean datatype flags compactly, as a series ofbit s. The bit field is stored in an integral type of known, fixed bit-width. Each Boolean flag is stored in a separate bit. Usually thesource code will define a set of constants, each a power of two, that semantically associate each individual bit with its respective Boolean flag. Thebitwise operator s and, or, and not are used in combination to set, reset and test the flags.A bit field is distinguished from a
bit array in that the latter is used to store a large set of bits indexed by integers and is often wider than any integral type supported by the language. Bit fields, on the other hand, typically fit within a machine word, and the denotation of bits is independent of their numerical index. Nevertheless, a bit field can be safely and elegantly implemented using a bit array where the bit indices for each flag are values of anenumerated type (like the Javadoc:SE|java/util|EnumSet class in Java); this avoids the dangers of direct bitwise manipulations.Examples
Example implementation in C:
Instead of using hardcoded numerical representations for the powers of two (
0x08
), the use of the bit shift operator (1 << 3
) with an incrementing shift operand is recommended for easier readability.Kernighan and Ritchie's book, "The C Programming Language", describes a method for defining and accessing fields directly. Using this method, bitwise operators are not needed as bit members can be accessed the same as struct members. An example using a
follows:struct However, bit members in structs have practical drawbacks. First, the ordering of bits in memory varies from
compiler to compiler. In addition, many popular compilers generate inefficient code for reading and writing bit members, and there are potentially severe thread safety issues relating to bit fields (especially on multiprocessor systems) due to the fact that most machines cannot manipulate arbitrary sets of bits in memory, but must instead load and store whole words. e.g. the following would not bethread-safe , in spite of the use of a mutex:clarifyme|date=July 2008as on most machines it is impossible at the hardware level to load and store
flag
andcounter
separately. (In order for this to be thread-safe, you would need to lock and unlock the mutex around all accesses to bothflag
andcounter
, even ifcounter
doesn't itself need to be thread-safe.). Similarly, through bitordering there is also a dependancy of the bitfields exact structure on the wordsize.ee also
*
Bitboard , used in chess and similar games.
*Bit array External links
* [http://articleworld.org/Bit_field Bit field] , a simple C example.
* [http://publications.gbdirect.co.uk/c_book/chapter6/bitfields.html Explanation from a book]
* [http://c2.com/cgi/wiki?BitField Description from another wiki]
* [http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=131 Use case in a C++ guide]
* [http://libbit.sourceforge.net/ bit library]
Wikimedia Foundation. 2010.