Basic Bitwise Operations in C++

Basic Bitwise Operators in C++.

BeginnerBitwise Operations ProgramsExample 1 of 5
basic-bitwise-operations.cpp
1#include <iostream>
2#include <bitset>
3using namespace std;
4
5void printBinary(int num) {
6 cout << bitset<8>(num) << " (" << num << ")" << endl;
7}
8
9int main() {
10 int a = 12; // 00001100
11 int b = 25; // 00011001
12
13 cout << "a = ";
14 printBinary(a);
15 cout << "b = ";
16 printBinary(b);
17 cout << endl;
18
19 // Bitwise AND
20 int andResult = a & b;
21 cout << "a & b (AND) = ";
22 printBinary(andResult);
23
24 // Bitwise OR
25 int orResult = a | b;
26 cout << "a | b (OR) = ";
27 printBinary(orResult);
28
29 // Bitwise XOR
30 int xorResult = a ^ b;
31 cout << "a ^ b (XOR) = ";
32 printBinary(xorResult);
33
34 // Bitwise NOT
35 int notResult = ~a;
36 cout << "~a (NOT) = ";
37 printBinary(notResult);
38
39 // Left shift
40 int leftShift = a << 2;
41 cout << "a << 2 (Left shift) = ";
42 printBinary(leftShift);
43
44 // Right shift
45 int rightShift = a >> 2;
46 cout << "a >> 2 (Right shift) = ";
47 printBinary(rightShift);
48
49 return 0;
50}

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)

What's going on

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.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <bitset>
Provides bitset for binary representation display.

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

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

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

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

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

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

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

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

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

return 0;

This ends the program successfully.