used if each 'field' were to be declared separately. For languages lacking native bit fields, or where the programmer wants 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. Examples C programming language Declaring a bit field in C and C++: // opaque and show #define YES 1 #define NO 0 // line styles #define SOLID 1 #define DOTTED 2 #define DASHED 3 // primary colors #define BLUE 0b100 #define GREEN 0b010 #define RED 0b001 // mixed colors #define BLACK 0 /* 000 */ #define YELLOW (RED | GREEN) /* 011 */ #define MAGENTA (RED | BLUE) /* 101 */ #define CYAN (GREEN | BLUE) /* 110 */ #define WHITE (RED | GREEN | BLUE) /* 111 */ const char* colors[8] = {"Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White"}; // bit field box properties struct BoxProps { unsigned int opaque : 1; unsigned int fill_color : 3; unsigned int : 4; // fill to 8 bits unsigned int show_border : 1; unsigned int border_color : 3; unsigned int border_style : 2; unsigned char : 0; // fill to nearest byte (16 bits) unsigned char width : 4, // Split a byte into 2 fields of 4 bits height : 4; }; 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 0b00000001 #define KEY_LEFT 0b00000010 #define KEY_DOWN 0b00000100 #define KEY_UP 0b00001000 #define KEY_START 0b00010000 #define KEY_SELECT 0b00100000 #define KEY_B 0b01000000 #define KEY_A 0b10000000 int gameControllerStatus = 0; /* Sets the gameControllerStatus using OR */ void KeyPressed( int key ) { gameControllerStatus |= key; } /* Clears the gameControllerStatus using AND and ~ (binary NOT)*/ void KeyReleased( int key ) { gameControllerStatus &= ~key; } /* Tests whether a bit is set using AND */ int IsPressed( int key ) { return gameControllerStatus & key; } Processor status register The status register of a processor is a bit field consisting of several flag bits. Each flag bit describes