Basic Bitwise Operations in C++
C++ bitwise operators with binary output: AND, OR, XOR, NOT, left and right shift on 12 and 25, and why ~12 prints as -13 in two's complement.
1#include <iostream>2#include <bitset>3using namespace std;45void printBinary(int num) {6 cout << bitset<8>(num) << " (" << num << ")" << endl;7}89int main() {10 int a = 12; // 0000110011 int b = 25; // 000110011213 cout << "a = ";14 printBinary(a);15 cout << "b = ";16 printBinary(b);17 cout << endl;1819 // Bitwise AND20 int andResult = a & b;21 cout << "a & b (AND) = ";22 printBinary(andResult);2324 // Bitwise OR25 int orResult = a | b;26 cout << "a | b (OR) = ";27 printBinary(orResult);2829 // Bitwise XOR30 int xorResult = a ^ b;31 cout << "a ^ b (XOR) = ";32 printBinary(xorResult);3334 // Bitwise NOT35 int notResult = ~a;36 cout << "~a (NOT) = ";37 printBinary(notResult);3839 // Left shift40 int leftShift = a << 2;41 cout << "a << 2 (Left shift) = ";42 printBinary(leftShift);4344 // Right shift45 int rightShift = a >> 2;46 cout << "a >> 2 (Right shift) = ";47 printBinary(rightShift);4849 return 0;50}
Sample Output
~a (NOT) = 11110011 (-13)
Low 8 bits shown; the int value is −13.
a << 2 (Left shift) = 00110000 (48)
12 × 4.
In this one
- What &, |, ^, ~, <<, and >> do bit by bit
- How to print binary with bitset
- Why ~12 is −13
- How shifts multiply and divide by powers of 2
How it works
- 1
a = 12 (00001100), b = 25 (00011001).
- 2
Print a & b, a | b, and a ^ b.
- 3
Print ~a: low byte 11110011, value −13.
- 4
Print a << 2 = 48 and a >> 2 = 3.
Dry run: Basic Bitwise Operations
00001100 (12)
- 1.& 00011001 (25)
- 2.= 00001000 (8)
- 3.| gives 00011101 (29)
- 4.^ gives 00010101 (21)
00001100 (12) & 00011001 (25) = 00001000 (8) | gives 00011101 (29) ^ gives 00010101 (21)
Edge cases
NOT on an unsigned byte
unsigned char c = 12; cout << (int)(unsigned char)~c;
Prints 243, because only 8 bits are kept and they are read as unsigned.
Shifting too far
int x = 1 << 32;
Shifting a 32-bit int by 32 or more is undefined behavior.
Right shift of a negative number
cout << (-16 >> 2);
Since C++20 this is defined as an arithmetic shift and gives −4; earlier it was implementation-defined, though compilers did the same.
What's going on
Bitwise operators work on the individual binary digits of an integer. This program applies each one to a = 12 and b = 25 and prints the results in binary with bitset<8>, so you can line the bits up and check them by eye.
12 is 00001100 and 25 is 00011001.
a & b (AND) keeps a bit only where both numbers have a 1. Only bit 3 is shared, so the result is 00001000 = 8.
a | b (OR) sets a bit where either number has a 1: 00011101 = 29.
a ^ b (XOR) sets a bit where the numbers differ: 00010101 = 21.
~a (NOT) flips every bit. The catch is that a is a 32-bit int, so all 32 bits flip, not just the 8 shown. The low 8 bits are 11110011, but the full value is negative. Signed integers use two's complement, where ~x always equals −x − 1, so ~12 = −13. The program prints that: 11110011 (−13). (An older version of this page showed 243, which is what you would get only if the value were an 8-bit unsigned number.)
a << 2 shifts every bit two places left and fills with zeros: 00110000 = 48. Each left shift multiplies by 2, so this is 12 × 4.
a >> 2 shifts two places right: 00000011 = 3. Each right shift divides by 2 and drops the remainder, so 12 ÷ 4 = 3.
bitset<8>(num) shows only the lowest 8 bits. Use bitset<32> to see the whole int, including all the leading 1s of ~a.
The headers come first. <iostream> is there because it provides cout and cin for input/output operations. <bitset> is there because it provides bitset for binary representation display. Leave one out and the compiler stops at the first name it does not recognize.
Understanding Bitwise Operations
Bitwise Concept:
vs Logical Operations:
Bitwise AND (&)
Operation:
a & b // AND operation
What happens here:
Truth Table:
Bitwise OR (|)
Operation:
a | b // OR operation
What happens here:
Truth Table:
Bitwise XOR (^)
Operation:
a ^ b // XOR operation
What happens here:
Truth Table:
Bitwise NOT (~)
Operation:
~a // NOT operation
What happens here:
Note:
Left Shift (<<)
Operation:
a << n // Left shift by n positions
What happens here:
Use Cases:
Right Shift (>>)
Operation:
a >> n // Right shift by n positions
What happens here:
Use Cases:
When to Use Bitwise Operations
Good fits:
Example Scenarios:
Important Considerations
Data Types:
Performance:
Readability:
return 0;
return 0; closes main and tells the operating system the program finished without an error.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is 6 ^ 3?
Pick an answer to check it.
Question 2
What does ~12 print for an int?
Pick an answer to check it.
Questions people ask
Is & the same as &&?
No. & works on each bit; && is logical AND on true/false and short-circuits.
Why use bitwise operators?
Flags, permissions, compact sets, hashing, graphics, and fast multiply or divide by powers of 2.
How do I print the full 32 bits?
cout << bitset<32>(x);