One of the C language’s strengths is that it can venture down into the depths of a computer’s inner workings. It loves to play in those dark, subterranean pools of raw data, bits, ports, and other primitive places that few high-level languages dare to tread.
Specifically, the C language is great at manipulating bit fields.
A bit field is where a byte, word, or larger chunk of data is manipulated one or more bits at a time. For example, an 8-bit byte (char) may represent 8 different binary values or toggles. This approach is more efficient than using 8 separate int variables to express on-off conditions.
You find bit fields used in legacy programs, dating back to when memory or storage was tight. Still, even when you have gigabytes available, sometimes packing a dozen or so conditions into a single int makes sense. In the online world, bit fields are used frequently because it takes less time to transmit a 32-bit word than it does to transmit 32 words.
An example of a bit field is the way colors are mapped in computer graphics. Three colors — red, green, and blue — are represented by three bits:
100
= Red
010
= Green
001
= Blue
As you run through the permutations of binary values 001
through 111
, you encounter the basic PC text graphics colors, as shown in the table below.
Binary | Hex/Dec | Color |
---|---|---|
000 | 0x00 / 0 | Black |
001 | 0x01 / 1 | Blue |
010 | 0x02 / 2 | Green |
011 | 0x03 / 3 | Cyan |
100 | 0x04 / 4 | Red |
101 | 0x05 / 5 | Magenta |
110 | 0x06 / 6 | Brown |
111 | 0x07 / 7 | White |
When the red/green/blue bits combine, they create the intermediate colors, such as blue and green (011
) make cyan. Blue and red (101
) create magenta. All bits off (000
) is black. All bits on (111
) is white.
A fourth bit on the original PC (1000
) set color intensity. When that bit is on, a second set of 8 colors is available, from gray (1000
or “bright black”) through bright white (1111
). The color “bright brown” (1110
) is yellow.
The text color bit field on the original PC was 8-bits wide in total. The first four bits set the background color, the second four set the text color.
As an early PC programmer, I eventually committed the bit field values to memory. I still remember that
0x04
is the text color red. In fact, the common acronym RGB helps me remember in which order the bits lie in the field: red, green, and blue.
PC text color is just one example of a bit field. In computerdom you’ll discover many bit fields. The C library uses bit fields in many functions and constants, but often macros are used to help manipulate them, so you don’t really get exposed directly. Regardless, bit fields allow for a lot of information to be contained in a tiny space.
As an example, suppose you’ve written a computer game. In the game, you use a bit field to track the status of a starship. The bit field looks like this:
Bit 0 – Transporter status
Bit 1 – Warp Drive
Bit 2 – Shields
Bit 3 – Phasers
Forgetting for a moment about Paramount Studios and their legions of lawyers defending copyrights, you need to code your game so that it can check the status of each of those bits, set a bit, and reset a bit. I’ll go over that process in next week’s Lesson. Before then, here are some bit field tidbits to ponder:
- Bits are referenced by their position within the byte, 0 through the width of the byte/word/long word.
- When looking at a binary representation of a bit field, bit 0 is on the far right.
- Generally speaking, a byte (char) is 8-bits wide.
- A word (short int) is 16-bits wide.
- A long word (int) is 32-bits wide.
- To set a bit is to assert its value to 1.
- To reset (or unset) a bit is to assert its value to 0.
- The value 1 is on or TRUE; the value 0 is off or FALSE.