#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;
}Output
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)
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 AND, OR, XOR operations
- Bitwise NOT (complement)
- Left shift and right shift operations
- Visual representation of bit operations
Bitwise operations enable efficient bit-level manipulation.
2. Header Files Used
-
#include <iostream>
- Provides cout and cin for input/output operations.
-
#include <bitset>
- Provides bitset for binary representation display.
3. Understanding Bitwise Operations
Bitwise Concept:
- Operations on individual bits
- Work at binary level
- More efficient than arithmetic
- Essential for low-level programming
vs Logical Operations:
- Bitwise: works on bits
- Logical: works on boolean values
- Different purposes
- Both important
4. Bitwise AND (&)
Operation:
a & b // AND operation
How it works:
- 1 if both bits are 1
- 0 otherwise
- Performed bit by bit
- Example: 12 & 25 = 8
Truth Table:
- 0 & 0 = 0
- 0 & 1 = 0
- 1 & 0 = 0
- 1 & 1 = 1
5. Bitwise OR (|)
Operation:
a | b // OR operation
How it works:
- 1 if either bit is 1
- 0 only if both are 0
- Performed bit by bit
- Example: 12 | 25 = 29
Truth Table:
- 0 | 0 = 0
- 0 | 1 = 1
- 1 | 0 = 1
- 1 | 1 = 1
6. Bitwise XOR (^)
Operation:
a ^ b // XOR operation
How it works:
- 1 if bits are different
- 0 if bits are same
- Performed bit by bit
- Example: 12 ^ 25 = 21
Truth Table:
- 0 ^ 0 = 0
- 0 ^ 1 = 1
- 1 ^ 0 = 1
- 1 ^ 1 = 0
7. Bitwise NOT (~)
Operation:
~a // NOT operation
How it works:
- Inverts all bits
- 0 becomes 1, 1 becomes 0
- One's complement
- Example: ~12 = 243 (for 8-bit)
Note:
- Result depends on data type size
- Signed vs unsigned matters
- Two's complement representation
8. Left Shift (<<)
Operation:
a << n // Left shift by n positions
How it works:
- Shifts bits left by n positions
- Fills right with zeros
- Effectively multiplies by 2^n
- Example: 12 << 2 = 48 (12 × 4)
Use Cases:
- Fast multiplication by powers of 2
- Bit manipulation
- Efficient calculations
9. Right Shift (>>)
Operation:
a >> n // Right shift by n positions
How it works:
- Shifts bits right by n positions
- Fills left (sign bit or zero)
- Effectively divides by 2^n
- Example: 12 >> 2 = 3 (12 ÷ 4)
Use Cases:
- Fast division by powers of 2
- Bit manipulation
- Efficient calculations
10. When to Use Bitwise Operations
Best For:
- Flags and masks
- Low-level programming
- Performance optimization
- Bit manipulation problems
- System programming
Example Scenarios:
- Setting/clearing flags
- Checking permissions
- Efficient calculations
- Cryptography
- Embedded systems
11. Important Considerations
Data Types:
- Works on integer types
- Signed vs unsigned matters
- Size affects results
- Be careful with signed shifts
Performance:
- Very fast operations
- Hardware-level efficiency
- Faster than arithmetic
- Optimized by CPU
Readability:
- Can be less readable
- Document complex operations
- Use for performance-critical code
- Balance clarity and speed
12. return 0;
This ends the program successfully.
Summary
- Bitwise operators: work on individual bits, enable efficient low-level operations.
- AND (&): 1 if both bits are 1, OR (|): 1 if either bit is 1.
- XOR (^): 1 if bits are different, NOT (~): inverts all bits.
- Left shift (<<): multiplies by 2^n, Right shift (>>): divides by 2^n.
- Understanding bitwise operations enables efficient bit manipulation.
- Essential for flags, masks, low-level programming, and optimization.
This program is fundamental for learning bit manipulation, understanding low-level operations, and preparing for system programming and optimization in C++ programs.