➕ C++ Operators
Estimated reading: 3 minutes 95 views

⚙️ C++ Bitwise Operators – Manipulate Data at the Binary Level


🧲 Introduction – What Are Bitwise Operators in C++?

Bitwise operators in C++ perform operations directly on the binary representation of integers. They are powerful tools for low-level programming, including embedded systems, performance optimization, and hardware-level applications.

🎯 In this guide, you’ll learn:

  • The list and purpose of bitwise operators
  • Syntax and usage examples
  • Bitwise shift operations
  • Common tricks and real-world applications

🧮 List of C++ Bitwise Operators

OperatorSymbolDescriptionExampleResult (Binary)
AND&Sets bits that are 1 in botha & b0000 0100
OR``Sets bits that are 1 in either`a
XOR^Sets bits that differa ^ b0000 1010
NOT~Inverts all bits~aComplemented
Left Shift<<Shifts bits left (multiplies)a << 10001 0000
Right Shift>>Shifts bits right (divides)a >> 10000 0010

Example values:
int a = 12; // 0000 1100
int b = 10; // 0000 1010


✅ Example – Bitwise Operators in Action

#include <iostream>
using namespace std;

int main() {
    int a = 12, b = 10;

    cout << "a & b = " << (a & b) << endl;
    cout << "a | b = " << (a | b) << endl;
    cout << "a ^ b = " << (a ^ b) << endl;
    cout << "~a = " << (~a) << endl;
    cout << "a << 1 = " << (a << 1) << endl;
    cout << "a >> 1 = " << (a >> 1) << endl;

    return 0;
}

🧠 Binary Representation Visual

DecimalBinary
1200001100
1000001010
AND (12 & 10): 00001000 = 8
OR  (12 | 10): 00001110 = 14
XOR (12 ^ 10): 00000110 = 6

📦 Bit Shifting Explained

🔁 Left Shift <<

int x = 5;           // 00000101
int result = x << 1; // 00001010 = 10

✅ Effectively multiplies by 2


🔁 Right Shift >>

int y = 8;           // 00001000
int result = y >> 1; // 00000100 = 4

✅ Effectively divides by 2


⚠️ Common Mistakes

❌ Mistake✅ Fix
Using bitwise operators on floatsBitwise ops work only with integers
Ignoring sign with right shiftUse unsigned types to avoid sign-extension surprises
Confusing & with &&& is bitwise AND; && is logical AND

🧠 Real-World Uses of Bitwise Operators

  • Masking specific bits int mask = 0x0F; // 00001111 result = value & mask;
  • Checking parity (even/odd) if (num & 1) cout << "Odd";
  • Fast multiplication/division by powers of 2
  • Permissions (bit flags in enums)

📌 Summary – Recap & Next Steps

🔍 Key Takeaways:

  • Bitwise operators allow direct manipulation of bits
  • Use &, |, ^, ~ for basic binary operations
  • Use << and >> for shifting (efficient multiplication/division)
  • Ideal for performance-critical and embedded programming

⚙️ Real-World Relevance:
Bitwise operations power digital systems, graphics engines, compression algorithms, and microcontroller code.


❓ FAQs – C++ Bitwise Operators

❓ Can bitwise operators be used with float or double?
❌ No. Use only with integers or cast to integer first.

❓ What does a << 1 do?
✅ Shifts all bits of a one position left — effectively a * 2.

❓ What is the difference between & and &&?
& is bitwise AND; && is logical AND.

❓ How do I set, clear, or toggle a bit?
✅ Use masks:

  • Set: num |= (1 << i)
  • Clear: num &= ~(1 << i)
  • Toggle: num ^= (1 << i)

❓ Is ~a the same as -a?
❌ No. ~a inverts all bits. -a is two’s complement negation.


Share Now :
Share

C++ Bitwise Operators

Or Copy Link

CONTENTS
Scroll to Top