Bit Field Manipulation

The three basic bit manipulation operations are:

  • Set a bit (change its value to 1)
  • Reset a bit (change its value to 0)
  • Read a bit (determine whether it’s 1 or 0)

The standard C library lacks specific functions to carry out these bit manipulations, and I haven’t checked to see whether a third party library is available. That’s because you can easily code these operations on your own.

To manipulate a bit within a byte, word, or long word, you employ the C language’s bitwise operators: & (and), | (or), ^ ( exclusive or), ~ (one’s compliment), and ! (not). Also coming in handy are the shift operators, << and >>.

Bitwise & and | share names and behaviors with their logical comparison counterparts, && and ||. The bitwise operators work at a binary level, however, which can be confusing. It helps to visualize how they work, but in a nutshell the | (or) operator can set bits and the & (and) operator can reset bits. The shift operators << and >> position bits within a value.

Of the three bit manipulation operations mentioned at the start of this post, the most basic is to set a bit. It requires the least amount of work. All you need to know is the bit number (its offset) and then perform a bitwise | (or) operation on that specific bit. The result is that the bit is set, as illustrated in Figure 1.

Figure 1. The bitwise | (or) operator sets a specific bit in a byte.

Figure 1. The bitwise | (or) operator sets a specific bit in a byte.

Pay attention to bit 2 in Figure 1. (It’s the third bit from the right.) The bitwise | (or) operation sets that bit. This operation holds true whether the original value of bit 2 is 1 or 0; the result always sets the bit to 1.

Here is a function to set a specific bit:

void bit_set(char bit, char *byte)
{
    bit = 1 << bit;
    *byte = *byte | bit;
}

Variable bit is the bit position, starting with 0 for the right-most bit. Variable byte is a pointer to the char variable manipulated. It’s a pointer so that a value doesn’t need to be returned from the function; the function manipulates the data directly.

The first line in the function sets the bit position. The value 1 (000000001) is shifted left << a given number of positions. When bit is zero, it’s not shifted. Otherwise, it ends up in the proper bit position (refer to Figure 1).

As an example, if the value of bit is 2, then the << operator positions the 1 at bit position 2, third from the right: 00000100.

The second line performs the bitwise | (or) operation, setting the bit at the proper position. Because byte is a pointer, the original value is manipulated and nothing needs to be returned.

bit_set(2,&status);

In the above statement, the second bit of variable status is set. If status equals 00000000, then after the call it equals 00000100.

As a more realistic example, suppose in your computer space game, the user desires to activate the starship’s shields. Within the code, you need to set the proper bit within the ships_status bit field variable. The bit’s position is defined as the constant SHIELDS:

bit_set(SHIELDS,&ships_status);

Elsewhere in the code, another function, bit_test(), might evaluate that condition. Yet another function, bit_reset() might turn it off, say if the Klingons brutally and stealthily attack. I’ll cover those functions in next week’s Lesson.

Leave a Reply