Learn Bitwise Operators in C++: A Simple Guide for Beginners

Aayush RawatAayush Rawat
4 min read

🚀 Introduction

In programming, we often need to work with data at the bit level for performance, optimization, and system-level tasks. Bitwise operators in C++ allow us to manipulate data in binary form, offering fast and efficient solutions to many problems.

This article will break down each bitwise operator in C++ with easy-to-understand examples. Wherever a concept gets tricky, we’ll also explain it in Hindi using English alphabets for better clarity.


🧰 Types of Bitwise Operators in C++

C++ provides the following bitwise operators:

OperatorSymbolMeaning
AND&Sets each bit to 1 if both bits are 1
ORSets each bit to 1 if at least one bit is 1
XOR^Sets each bit to 1 if bits are different
NOT~Inverts all bits
Left Shift<<Shifts bits to the left (×2 each time)
Right Shift\>>Shifts bits to the right (÷2 each time)

💡 Let's Understand Each with Examples

1️⃣ Bitwise AND (&)

int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
cout << (a & b); // Output: 1 (0001)

Explanation: Only the bits where both a and b have 1 will result in 1.
Hindi: & operator tabhi 1 return karta hai jab dono bits 1 hoon.


2️⃣ Bitwise OR (|)

int a = 5; // 0101
int b = 3; // 0011
cout << (a | b); // Output: 7 (0111)

Even if one bit is 1, the result is 1.
Hindi: Agar kisi bhi ek position pe 1 hai to result 1 hoga.


3️⃣ Bitwise XOR (^)

int a = 5;  // 0101
int b = 3;  // 0011
cout << (a ^ b);  // Output: 6 (0110)

Explanation: If the bits are different, the result is 1.
Hindi: XOR ka simple rule hai – agar bits alag hain to 1, warna 0.


4️⃣ Bitwise NOT (~)

int a = 5;  // 0000 0101
cout << ~a; // Output: -6

Why -6?
It inverts all bits and then converts the result into 2's complement form.
Hindi: Jab aap ~ use karte ho, to saare bits flip ho jaate hain (1→0, 0→1), aur fir result ko negative form mein represent kiya jaata hai.


5️⃣ Left Shift (<<)

int a = 5;  // 0000 0101
cout << (a << 1); // Output: 10 (0000 1010)

Shifting left by 1 means multiplying by 2.
Hindi: Left shift se value 2 se multiply ho jaati hai.


6️⃣ Right Shift (>>)

int a = 5;  // 0000 0101
cout << (a >> 1); // Output: 2 (0000 0010)

Shifting right by 1 divides the number by 2 (ignoring the remainder).
Hindi: Right shift se value 2 se divide hoti hai, lekin decimal part ignore ho jaata hai.


🎯 Real-World Use Cases

Bitwise operators are often used in:

  • Competitive programming

  • Embedded systems and hardware communication

  • Data compression and encryption

  • Performance optimization in logic-heavy applications

Hindi: Bitwise operators ka use tab hota hai jab aapko low-level performance chahiye ya memory optimize karni ho.


🧾 Summary

OperatorDescription
&AND – Both bits must be 1 to get 1
``OR – If at least one of the bits is 1, otherwise 0
^XOR – Bits must differ to get 1
~NOT – Inverts all bits
<<Left Shift – Multiplies by 2
>>Right Shift – Divides by 2

🙌 Final Words

Understanding bitwise operators takes your programming skills to the next level. They're fast, powerful, and essential for many real-world problems.

Let me know in the comments if you’d like a practice problem guide or Bitwise Tricks next!
Hindi: Agar aap chahein to main ek practice article bhi likh sakta hoon jisme bitwise questions ko step-by-step solve kiya jaaye.


📌 This article is part of my DSA in C++ – Hindi Friendly Series.

👉 Stay tuned for the next topic in this series!

🔔 Follow me to get notified when the next post is live.

0
Subscribe to my newsletter

Read articles from Aayush Rawat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aayush Rawat
Aayush Rawat