Bit field

Bit field

A bit field is a common idiom used in computer programming to store a set of Boolean datatype flags compactly, as a series of bits. The bit field is stored in an integral type of known, fixed bit-width. Each Boolean flag is stored in a separate bit. Usually the source code will define a set of constants, each a power of two, that semantically associate each individual bit with its respective Boolean flag. The bitwise operators 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 an enumerated type (like the Javadoc:SE|java/util|EnumSet class in Java); this avoids the dangers of direct bitwise manipulations.

Examples

Example implementation in C:
#define PREFERENCE_LIKES_ICE_CREAM ((unsigned char) (1 << 0)) // 0x01
#define PREFERENCE_PLAYS_GOLF ((unsigned char) (1 << 1)) // 0x02
#define PREFERENCE_WATCHES_TV ((unsigned char) (1 << 2)) // 0x04
#define PREFERENCE_READS_BOOKS ((unsigned char) (1 << 3)) // 0x08

unsigned char preference;

void set_preference(unsigned char flag) { preference |= flag;}

void reset_preference(unsigned char flag) { preference &= ~flag;}

unsigned char get_preference(unsigned char flag) { return (preference & flag) != 0;}

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 struct follows:

struct preferences { unsigned int likes_ice_cream : 1; unsigned int plays_golf : 1; unsigned int watches_tv : 1; unsigned int reads_books : 1;};

struct preferences fred;

fred.likes_ice_cream = 1;fred.plays_golf = 1;fred.watches_tv = 1;fred.reads_books = 0;

if (fred.likes_ice_cream = 1) /* ... */

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 be thread-safe, in spite of the use of a mutex:clarifyme|date=July 2008

struct foo { int flag : 1; int counter : 15;};

struct foo my_foo;

/* ... */

pthread_mutex_lock(&my_mutex);my_foo.

as on most machines it is impossible at the hardware level to load and store flag and counter separately. (In order for this to be thread-safe, you would need to lock and unlock the mutex around all accesses to both flag and counter, even if counter 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.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • bit field — noun a field containing only binary characters • Topics: ↑computer science, ↑computing • Hypernyms: ↑field …   Useful english dictionary

  • Bit slicing — is a technique for constructing a processor from modules of smaller bit width. Each of these components processes one bit field or slice of an operand. The grouped processing components would then have the capability to process the chosen full… …   Wikipedia

  • Bit array — A bit array (or bitmap, in some cases) is an array data structure which compactly stores individual bits (boolean values). It implements a simple set data structure storing a subset of {1,2,..., n } and is effective at exploiting bit level… …   Wikipedia

  • Field Target — is an outdoor air gun discipline originating in the United Kingdom, on the late 1980s, but gaining popularity worldwide.UK RulesIn UK (United Kingdom) rules, competitors aim to shoot the small “kill” zone that forms part of a larger metal… …   Wikipedia

  • Bit-string physics — is an emerging body of theory which considers the universe to be a process of operations on strings of bits. Bit string physics is often associated with A.F. Parker Rhodes combinatorial hierarchy, which is notable for its relationship with the… …   Wikipedia

  • Bit rot — Bit rot, also known as bit decay, data rot, or data decay, is a colloquial computing term used to describe either a gradual decay of storage media or the degradation of a software program over time. The latter use of the term implies that… …   Wikipedia

  • Field Programmable Nanowire Interconnect — (often abbreviated FPNI) is a new computer architecture developed by Hewlett Packard. This is a defect tolerant architecture, using the results of the Teramac experiment.Details: The design combines a nanoscale crossbar switch structure with… …   Wikipedia

  • Field Records — is a British, Nottingham based independent record label featuring bands such as Ann Arbor, Karhide, LaFaro, Public Relations Exercise, Princess, and Maybeshewill.The label is run by Tim Waterfield.ReleasesCDs*Field001 Ann Arbor 16 bit *Field002… …   Wikipedia

  • Field (computer science) — In computer science, data that has several parts can be divided into fields. Relational databases arrange data as sets of database records, also called rows. Each record consists of several fields; the fields of all records form the columns. In… …   Wikipedia

  • Field encapsulation — In computer programming, field encapsulation, also called data hiding, involves providing methods that can be used to read/write to/from the field rather than accessing the field directly. Sometimes these accessor methods are called getX and setX …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”