How to set, clear, and toggle a single bit

ghz 1years ago ⋅ 7059 views

Question

How can I set, clear, and toggle a bit?


Answer

Setting a bit

Use the bitwise OR operator (|) to set nth bit of number to 1.

// Can be whatever unsigned integer type you want, but
// it's important to use the same type everywhere to avoid
// performance issues caused by mixing integer types.
typedef unsigned long Uint;

// In C++, this can be template.
// In C11, you can make it generic with _Generic, or with macros prior to C11.
inline Uint bit_set(Uint number, Uint n) {
    return number | ((Uint)1 << n);
}

Note that it's undefined behavior to shift by more than the width of a Uint. The same applies to all remaining examples.

Clearing a bit

Use the bitwise AND operator (&) to set the nth bit of number to 0.

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

You must invert the bit string with the bitwise NOT operator (~), then AND it.

Toggling a bit

Use the bitwise XOR operator (^) to toggle the nth bit of number.

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

Checking a bit

You didn't ask for this, but I might as well add it.

To check a bit, shift number n to the right, then bitwise AND it:

// bool requires #include <stdbool.h> prior to C23
inline bool bit_check(Uint number, Uint n) {
    return (number >> n) & (Uint)1;
}

Changing the n th bit to x

There are alternatives with worse codegen, but the best way is to clear the bit like in bit_clear, then set the bit to value, similar to bit_set.

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

All solutions have been tested to provide optimal codegen with GCC and clang. See https://godbolt.org/z/Wfzh8xsjW.