Skip to main content
Numverto numverto
binary programming bitwise

Bitwise Operators in Programming — AND, OR, XOR, NOT Explained with Examples

Learn how bitwise AND, OR, XOR, NOT, left shift, and right shift operators work with binary examples in C, Java, and Python.

Numverto Editorial Team Numverto Editorial Team 5 min read Editorial standards

Introduction

Bitwise operators are among the most powerful yet misunderstood tools in programming. They operate directly on individual bits of numbers, making them extremely fast — often completing in a single CPU cycle. If you are studying for BCA, BTech, or GATE, or working on embedded systems, graphics, or cryptography, bitwise operations are essential knowledge.

🔧 Practice binary math: Binary Arithmetic Calculator — perform addition, subtraction, and see bit-level operations step by step.

All 6 Bitwise Operators

OperatorSymbol (C/Java/Python)Description
AND&Both bits must be 1 → result is 1
ORpipe symbolAt least one bit is 1 → result is 1
XOR^Bits are different → result is 1
NOT~Flips every bit (0↔1)
Left Shiftleft-shiftShifts bits left, fills with 0
Right Shiftright-shiftShifts bits right, drops rightmost

Truth Tables

AND (&)

ABA & B
000
010
100
111

OR (|)

ABA | B
000
011
101
111

XOR (^)

ABA ^ B
000
011
101
110

Worked Examples

AND: 0b1010 & 0b1100

  1010
& 1100
──────
  1000  = decimal 8

Only position 3 has both bits set.

OR: 0b1010 | 0b1100

  1010
| 1100
──────
  1110  = decimal 14

XOR: 0b1010 ^ 0b1100

  1010
^ 1100
──────
  0110  = decimal 6

Bits that differ become 1.

NOT: ~0b00001010 (8-bit)

~ 00001010
──────────
  11110101  = decimal 245 (unsigned) or -11 (signed)

Left Shift: 5 left-shifted by 2

5 = 0000 0101, shift left by 2: Result: 0001 0100 = 20

Left shift by n = multiply by 2ⁿ. (5 × 4 = 20)

Right Shift: 20 right-shifted by 2

20 = 0001 0100, shift right by 2: Result: 0000 0101 = 5

Right shift by n = integer divide by 2ⁿ. (20 ÷ 4 = 5)

Real-World Uses

  • Check if number is even/odd: n & 1 → 0 = even, 1 = odd (faster than n % 2)
  • Flag systems: Unix permissions use bits (read=4, write=2, execute=1)
  • Color manipulation: Extract RGB channels from hex color values
  • Cryptography: XOR is fundamental to encryption algorithms
  • Swap without temp variable: a ^= b; b ^= a; a ^= b;
  • Power of 2 check: n & (n-1) == 0 means n is a power of 2

Python Examples

a = 0b1010  # 10
b = 0b1100  # 12

print(bin(a & b))   # 0b1000 (8)
print(bin(a | b))   # 0b1110 (14)
print(bin(a ^ b))   # 0b110  (6)
print(bin(~a))      # -0b1011 (-11 in signed)
print(bin(a * 2))   # 0b10100 (20) — same as left shift by 1
print(bin(a // 2))  # 0b101   (5) — same as right shift by 1

Common Mistakes

  1. Confusing & (bitwise) with && (logical) — in C/Java, & operates on bits, && on booleans
  2. Forgetting NOT depends on bit width~5 in 8-bit vs 32-bit gives different results
  3. Shift overflow — shifting by more than the bit width is undefined in C
  4. XOR is NOT addition — 1^1=0, not 10
  5. Operator precedence — bitwise operators have lower precedence than comparison; use parentheses

Frequently Asked Questions

What does XOR do?

XOR (exclusive OR) returns 1 only when the two bits are different. If both are 0 or both are 1, the result is 0. XOR is commonly used in encryption, checksums, and toggling bits.

Left shift by n positions multiplies the number by 2ⁿ. For example, 5 left-shifted by 3 = 5 × 8 = 40. This is faster than actual multiplication on hardware.

Why are bitwise operators faster than arithmetic?

CPUs have dedicated bitwise logic gates that execute in a single clock cycle. Arithmetic operations (especially multiplication/division) often take multiple cycles.

What is bit masking?

Bit masking uses AND with a pattern to extract specific bits. For example, color & 0xFF extracts the blue channel from a 24-bit RGB value.

How does XOR swap work without a temporary variable?

a ^= b; b ^= a; a ^= b; works because XOR is self-inverse: x ^ x = 0 and x ^ 0 = x. Each step cancels out one of the original values.

Advertisement
Share: Twitter LinkedIn Facebook

Free tools

Learn faster with Numverto

Free number system converters, binary tools, EMI calculators, and more, with step-by-step working.

Written by

Numverto Editorial Team

Numverto Editorial Team

Founder of Numverto. MCA graduate, full-stack developer, and lifelong learner who built this platform so every student gets the explanation, not just the answer.

Related tools

Advertisement

More from Numverto Blog

Discussion

Comments

Popular Tools

View all 25 free tools → · Read tutorials · Number system guide