Understanding Bit Manipulation in C++

Bit manipulation is a crucial aspect of programming, especially in systems programming and low-level languages like C and C++. One fundamental operation is the ability to manipulate individual bits of a number. This can enhance performance and provide detailed control over data representation and operations.

In this blog post, we will explore setting, clearing, and toggling a single bit in an integer using C++. We will also touch upon checking a bit’s value and modifying a bit based on its state.

Setting a Bit

To set an nth bit of a number to 1, we can use the bitwise OR (|) operator. This operator allows us to combine two bit patterns, resulting in a 1 wherever either operand has a 1.

Here’s how you can implement this:

typedef unsigned long Uint;

inline Uint bit_set(Uint number, Uint n) {
    return number | ((Uint)1 << n);
}

Important Note:

Be cautious not to shift a bit position greater than the width of Uint, as this leads to undefined behavior.

Clearing a Bit

To clear an nth bit (set it to 0), we utilize the bitwise AND (&) operator. First, we need to invert the bit using the bitwise NOT (~) operator. Here’s the implementation:

inline Uint bit_clear(Uint number, Uint n) {
    return number & ~((Uint)1 << n);
}

Toggling a Bit

Sometimes, you may need to change the state of a bit from 1 to 0 or from 0 to 1. This operation is known as toggling, and it can be efficiently achieved with the bitwise XOR (^) operator:

inline Uint bit_toggle(Uint number, Uint n) {
    return number ^ ((Uint)1 << n);
}

Checking a Bit

While not asked initially, it’s essential to know how to check if a specific bit is 1 or 0. Here’s how you can implement this using bitwise operations:

inline bool bit_check(Uint number, Uint n) {
    return (number >> n) & (Uint)1;
}

Changing the n-th Bit to x

If you want to change the nth bit to a specific value x (0 or 1), the best approach is to first clear the bit (using bit_clear) and then set it to the desired value (using bit_set). Here’s a simple function to achieve this:

inline Uint bit_set_to(Uint number, Uint n, bool x) {
    return (number & ~((Uint)1 << n)) | ((Uint)x << n);
}

These functions have been tested for optimal code generation using compilers like GCC and clang. You can see their performance on Godbolt.

In summary, understanding and applying these bit manipulation techniques enables you to write more efficient and effective code in C++. Mastering them can empower you to tackle more complex problems with ease!