Basic Bitwise Operations
Basic Bitwise Operators in C++
C++ Basic Bitwise Operations Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <bitset>
using namespace std;
void printBinary(int num) {
cout << bitset<8>(num) << " (" << num << ")" << endl;
}
int main() {
int a = 12; // 00001100
int b = 25; // 00011001
cout << "a = ";
printBinary(a);
cout << "b = ";
printBinary(b);
cout << endl;
// Bitwise AND
int andResult = a & b;
cout << "a & b (AND) = ";
printBinary(andResult);
// Bitwise OR
int orResult = a | b;
cout << "a | b (OR) = ";
printBinary(orResult);
// Bitwise XOR
int xorResult = a ^ b;
cout << "a ^ b (XOR) = ";
printBinary(xorResult);
// Bitwise NOT
int notResult = ~a;
cout << "~a (NOT) = ";
printBinary(notResult);
// Left shift
int leftShift = a << 2;
cout << "a << 2 (Left shift) = ";
printBinary(leftShift);
// Right shift
int rightShift = a >> 2;
cout << "a >> 2 (Right shift) = ";
printBinary(rightShift);
return 0;
}a = 00001100 (12) b = 00011001 (25) a & b (AND) = 00001000 (8) a | b (OR) = 00011101 (29) a ^ b (XOR) = 00010101 (21) ~a (NOT) = 11110011 (243) a << 2 (Left shift) = 00110000 (48) a >> 2 (Right shift) = 00000011 (3)
Understanding Basic Bitwise Operations
This program teaches you Basic Bitwise Operations in C++. Bitwise operators work directly on individual bits of numbers, enabling efficient low-level operations. Understanding bitwise operations is essential for system programming, optimization, and solving many algorithmic problems.
---
1. What This Program Does
The program demonstrates basic bitwise operations:
Bitwise operations enable efficient bit-level manipulation.
---
2. Header Files Used
---
3. Understanding Bitwise Operations
Bitwise Concept
:
vs Logical Operations
:
---
4. Bitwise AND (&)
Operation
:
a & b // AND operation
How it works
:
Truth Table
:
---
5. Bitwise OR (|)
Operation
:
a | b // OR operation
How it works
:
Truth Table
:
---
6. Bitwise XOR (^)
Operation
:
a ^ b // XOR operation
How it works
:
Truth Table
:
---
7. Bitwise NOT (~)
Operation
:
~a // NOT operation
How it works
:
Note
:
---
8. Left Shift (<<)
Operation
:
a << n // Left shift by n positions
How it works
:
Use Cases
:
---
9. Right Shift (>>)
Operation
:
a >> n // Right shift by n positions
How it works
:
Use Cases
:
---
10. When to Use Bitwise Operations
Best For
:
Example Scenarios
:
---
11. Important Considerations
Data Types
:
Performance
:
Readability
:
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning bit manipulation, understanding low-level operations, and preparing for system programming and optimization in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Basic Bitwise Operations
This C++ program is part of the "Bitwise Operations Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.