A bit field is a data structure used in computer programming. It consists of a number of adjacent computer memory locations which have been allocated to hold a sequence of bits, stored so that any single bit or group of bits within the set can be addressed. A bit field is most commonly used to represent integral types of known, fixed bit-width. The meaning of the individual bits within the field is determined by the programmer; for example, the first bit in a bit field is sometimes used to determine the state of a particular attribute associated with the bit field. Within microprocessors and other logic devices, collections of bit fields called "flags" are commonly used to control or to indicate the intermediate state or outcome of particular operations. Microprocessors typically have a status register that is composed of such flags, used to indicate various post-operation conditions, for example an arithmetic overflow. The flags can be read and used to decide subsequent operations, such as in processing conditional jump instructions. For example, a JE... instruction in the x86 assembly language will result in a jump if the Z flag was set by some previous operation. 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.
Implementation
Bit fields can be used to reduce memory consumption when a program requires a number of integer variables which always will have low values. For example, in many systems storing an integer value requires two bytes of memory; sometimes the values to be stored actually need only one or two bits. Having a number of these tiny variables share a bit field allows efficient packaging of data in the memory. In C and C++, native implementation-defined bit fields can be created using unsigned int, signed int, or _Bool. In this case, the programmer can declare a structure for a bit field which labels and determines the width of several subfields. Adjacently declared bit fields of the same type can then be packed by the compiler into a reduced number of words, compared with the memory used if each 'field' were to be declared separately. For languages lacking native bitfields, or where the programmer wants strict control over the resulting bit representation, it is possible to manually manipulate bits within a larger word type. In this case, the programmer can set, test, and change the bits in the field using combinations of masking and bitwise operations.
const char* colors = ; // bit field box properties struct BoxProps
The layout of bit fields in a C struct is implementation-defined. For behavior that remains predictable across compilers, it may be preferable to emulate bit fields with a primitive and bit operators: /* Each of these preprocessor directives defines a single bit, corresponding to one button on the controller. Button order matches that of the Nintendo Entertainment System. */
define KEY_RIGHT /* 00000001 */
define KEY_LEFT /* 00000010 */
define KEY_DOWN /* 00000100 */
define KEY_UP /* 00001000 */
define KEY_START /* 00010000 */
define KEY_SELECT /* 00100000 */
define KEY_B /* 01000000 */
define KEY_A /* 10000000 */
int gameControllerStatus = 0; /* Sets the gameControllerStatus using OR */ void KeyPressed /* Turns the key in gameControllerStatus off using AND and ~ */ void KeyReleased /* Tests whether a bit is set using AND */ int IsPressed
Processor status register
A simple example of a bitfield status register is included in the design of the eight-bit 6502 processor. One eight-bit field held seven pieces of information:
A subset of flags in a flag field may be extracted by ANDing with a mask. In addition, a large number of languages, due to the shift operator's use in performing power-of-two exponentiation, also support the use of the shift operator in combination with the AND operator to determine the value of one or more bits. Suppose that the status-byte 103 is returned, and that within the status-byte we want to check the 5th flag bit. The flag of interest is the 5th one - so the mask-byte will be. ANDing the status-byte 103 with the mask-byte 32 evaluates to 32, our original mask-byte, which means the flag bit is set; alternatively, if the flag-bit had not been set, this operation would have evaluated to 0. Thus, to check the nth bit from a variable v, we can perform the operation: bool nth_is_set = != 0; bool nth_is_set = & 1;
Changing bits in flag words
Writing, reading or toggling bits in flags can be done only using the OR, AND and NOT operations - operations which can be performed quickly in the processor. To set a bit, OR the status byte with a mask byte. Any bits set in the mask byte or the status byte will be set in the result. To toggle a bit, XOR the status byte and the mask byte. This will set a bit if it is cleared or clear a bit if it is set.