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
Share this article
Learn Faster with Numverto
Explore free number system converters, binary tools, EMI calculators, GST calculators, and educational guides.
About Numverto
Numverto Editorial Team
Numverto publishes educational content about number systems, computer science concepts, binary arithmetic, financial calculations, EMI formulas, GST calculations, and practical learning resources for students and professionals.
Article Metadata
Tags: binary, programming, bitwise, computer science
Last Updated: June 2026
Related Calculators
Related Articles
22 June 2026
Hexadecimal to Binary Conversion — Step by Step Guide with Reference Table
Learn how to convert hexadecimal to binary using the nibble expansion method. Includes a complete hex-to-binary reference table, worked examples, and a free converter tool.
Read article →22 June 2026
IP Address in Binary — How IPv4 Addresses Work with Binary
Learn how IPv4 addresses are represented in binary, how subnet masks work, and why networking uses binary. Includes conversion examples and reference table.
Read article →21 June 2026
Two's Complement Subtraction — Step by Step Guide
Learn how to perform binary subtraction using two's complement method. Step-by-step examples, reference table, and common mistakes for computer science students.
Read article →