Basic Bitwise Operations

Basic Bitwise Operators in C++

BeginnerTopic: Bitwise Operations Programs
Back

C++ Basic Bitwise Operations Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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)

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 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

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#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.

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.

Table of Contents