C++ Bitwise Operators
In this page:
result = value1 & value2; // AND
result = value1 | value2; // OR
result = value1 ^ value2; // XOR
result = ~value; // NOT
result = value << n; // shift left
result = value >> n; // shift right
Bitwise AND and OR
Bitwise & and | operate on the individual binary digits of a number rather than its value as a whole: & sets each output bit to 1 only where both operands have a 1 in that position, while | sets it to 1 if either operand does.
These are commonly used to check or combine flags packed into a single integer, one bit per flag.
Example: Bitwise AND and OR
#include <iostream>
int main() {
int a = 6, b = 3; // 110 and 011
std::cout << (a & b) << std::endl; // 010 = 2
std::cout << (a | b) << std::endl; // 111 = 7
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bitwise XOR
Bitwise ^ (XOR) sets each output bit to 1 exactly where the two operands' bits differ, and to 0 where they match.
A distinctive property of XOR is that applying it twice with the same value undoes the first application, which is the basis of some simple encryption and value-swapping tricks.
Example: Bitwise XOR
#include <iostream>
int main() {
int a = 5, b = 3;
int x = a ^ b;
std::cout << x << std::endl;
std::cout << (x ^ b) << std::endl; // XOR twice undoes it: back to a
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bitwise NOT
Bitwise ~ (NOT) is a unary operator that flips every bit of its operand, turning every 1 into a 0 and every 0 into a 1 — this is also called the one's complement.
It's frequently used to build a bitmask that clears specific bits when combined with &, since ~mask inverts exactly the bits you want to preserve or discard.
Example: Bitwise NOT
#include <iostream>
int main() {
unsigned char a = 5; // 00000101
std::cout << (int)(unsigned char)(~a) << std::endl; // 250: every bit flipped
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Left Shift and Right Shift
<< shifts every bit in a number to the left by the given number of positions, filling the vacated low bits with 0 — and because binary place values double with each position, shifting left by 1 is equivalent to multiplying by 2.
>> shifts right the same way, which is equivalent to integer division by 2 for each position shifted.
Example: Left Shift and Right Shift
#include <iostream>
int main() {
int x = 4;
std::cout << (x << 1) << std::endl; // 8: multiply by 2
std::cout << (x >> 1) << std::endl; // 2: divide by 2
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bitwise Compound Assignment
Just like arithmetic operators, bitwise operators have compound assignment forms — &=, |=, ^=, <<=, and >>= — that apply the operation directly to a variable and store the result back into it.
These show up constantly in low-level code that manages hardware registers or packed flag fields, where updating a few specific bits without disturbing the rest is a routine operation.
Example: Bitwise Compound Assignment
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare flags and set it to 0b0110
int flags = 0b0110;
flags &= 0b0100;
// Print to the console with cout
std::cout << flags << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
&when you meant&&, which gives a bitwise result instead of a true/false test. - Shifting by a negative amount or by the width of the type or more, like
1 << 32on a 32-bit int, which is undefined behavior. - Forgetting that
~5flips every bit and gives -6 for signed ints, rather than 0 or 'not 5' as a logical result.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: