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.
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
| Operator | Symbol (C/Java/Python) | Description |
|---|---|---|
| AND | & | Both bits must be 1 → result is 1 |
| OR | pipe symbol | At least one bit is 1 → result is 1 |
| XOR | ^ | Bits are different → result is 1 |
| NOT | ~ | Flips every bit (0↔1) |
| Left Shift | left-shift | Shifts bits left, fills with 0 |
| Right Shift | right-shift | Shifts bits right, drops rightmost |
Truth Tables
AND (&)
| A | B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
OR (|)
| A | B | A | B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
XOR (^)
| A | B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
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 thann % 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) == 0means 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
- Confusing
&(bitwise) with&&(logical) — in C/Java,&operates on bits,&&on booleans - Forgetting NOT depends on bit width —
~5in 8-bit vs 32-bit gives different results - Shift overflow — shifting by more than the bit width is undefined in C
- XOR is NOT addition — 1^1=0, not 10
- 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.
How is left shift related to multiplication?
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.
Related Calculators
Related Articles
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
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
More from Numverto Blog
ieee754
IEEE 754 Floating Point Explained Sign, Exponent & Mantissa
Master IEEE 754 floating point format sign, exponent, mantissa explained with real conversion examples for exams and coding interviews.
13 July 2026
IEEE 754 Standard
Why Does 0.1 + 0.2 Equal 0.30000000000000004? Floating-Point Errors Explained
Understand why computers produce floating-point precision errors, how the IEEE 754 standard stores decimal numbers in binary, why rounding occurs, and the best practices developers use to write accurate, reliable code.
1 July 2026
Discussion