Bitwise Operations in C++: How to Set, Clear, and Toggle a Single Bit (Without Losing Your Mind)
Hello, colleague! If you've ever looked at someone else's C++ code and seen strange symbols like |, &, and ~, and then wondered "why are they doing that?" — you've come to the right place. Today we'll break down one of the most popular (and, frankly, most elegant) topics in programming: bitwise operations. Specifically — how to set, clear, and toggle a single bit.
This isn't just an academic exercise. Working with bits is a superpower that will come in handy everywhere: from working with configuration flags and microcontroller peripherals to optimizing data storage in games or databases. On Stack Overflow, this question has garnered hundreds of thousands of views — meaning it's truly important.
Let's skip the boring theory and get straight to business. Let's go!
Why Do We Work with Bits at All?
Imagine you have 8 different settings (e.g., enable sound, show tooltips, enable night mode, etc.). You could store them in eight separate bool variables. But that's 8 bytes (or more). Alternatively, you could use one byte — 8 bits, each responsible for its own setting. That's an 8x memory savings — and that's just the beginning.
Bitwise operations are low-level magic that runs incredibly fast. The processor executes them in a single clock cycle. For embedded systems, drivers, and high-performance applications, this is critical.
In C++, three main operators are used for bit manipulation:
|(bitwise OR) — to set a bit.&(bitwise AND) — to clear a bit.^(bitwise exclusive OR, XOR) — to toggle a bit.
And one more useful operator — ~ (bitwise NOT), which inverts all bits. We'll need it for clearing.
Bit Numbering: Let's Agree Right Away
In programming, bits are conventionally numbered starting from zero. The least significant bit (LSB) is bit 0. The most significant bit (MSB) is bit 7 for a byte, bit 15 for a word (16 bits), and so on.
In this article, we'll use uint8_t (8-bit unsigned integer) for clarity. However, all examples work with int, unsigned int, uint32_t, and any other integer types.
How to Set a Bit
Setting a bit means making it equal to 1, regardless of its previous value.
The simplest way is to use bitwise OR with a mask. A mask is a number in which only the bit we want to change is set.
Formula:
number |= (1 << bit_number);Let's break it down step by step:
1 << bit_number— shifts a 1 left by the required number of positions. If the bit number is 3, then1 << 3gives the binary00001000.number | mask— the OR operation will set the bit to 1 if it was 0, and leave it as 1 if it was already 1.
C++ Code Example:
#include <iostream>#include <cstdint>
int main() { uint8_t flags = 0b00000000; // All bits initially cleared
// Set the 3rd bit (counting from zero) flags |= (1 << 3);
// Now flags = 0b00001000 std::cout << "After setting bit 3: " << static_cast<int>(flags) << std::endl;
// Set the 0th bit flags |= (1 << 0); // flags = 0b00001001 std::cout << "After setting bit 0: " << static_cast<int>