
Bitwise Operators in C Language: Complete Guide with Examples [Step-by-Step]
📋 Bitwise Operators in C - Quick Overview
| Operator | Symbol | Description |
|---|---|---|
| AND | & | Returns 1 if both bits are 1 |
| OR | | | Returns 1 if at least one bit is 1 |
| XOR | ^ | Returns 1 if bits are different |
| Left Shift | << | Shifts bits left (multiply by 2) |
| Right Shift | >> | Shifts bits right (divide by 2) |
| Complement | ~ | Flips all bits (1's complement) |
📑 What You'll Learn in This Tutorial
- ✅ Bitwise AND Operator (&)
- ✅ Bitwise OR Operator (|)
- ✅ Bitwise XOR Operator (^)
- ✅ Left Shift Operator (<<)
- ✅ Right Shift Operator (>>)
- ✅ One's Complement Operator (~)
Ever wondered how your computer processes millions of calculations per second? The secret lies in bitwise operations — the language of computers themselves.
Bitwise operators are a universal concept — whether you're studying at MIT, Imperial College, or the University of Melbourne, you'll encounter them. They're the same in Silicon Valley, Bengaluru, or Berlin. That's because C is a global language, and bitwise operations are at its heart.
I've taught this to students from over 30 countries, and I've seen the same "aha!" moment happen when it finally clicks. Today, I'll help you get there too — no matter where you're learning from.
Bitwise operators in C are special operators that perform operations at the data bit level. When operations are performed on individual bits, it's called bit-level programming. These operations are much faster than normal arithmetic operations because they're directly supported by the CPU's arithmetic logic unit (ALU) — and this is true whether you're using GCC on Linux in Europe, Clang on macOS in California, or Turbo C on Windows in Asia.
C language uses six types of bitwise operators:
- & — Bitwise AND Operator
- | — Bitwise OR Operator
- ^ — Bitwise XOR (Exclusive OR) Operator
- << — Bitwise Left Shift Operator
- >> — Bitwise Right Shift Operator
- ~ — One's Complement Operator
🎓 From Our Experience
Here's what we've observed teaching this topic to students worldwide:
- Common Mistake: Many students confuse bitwise AND (
&) with logical AND (&&). I remember a student from Canada who spent hours debugging a program, only to realize he'd used&&instead of&. Now I tell all my students — whether they're in London, Lagos, or Los Angeles — "If you're checking two conditions, use&&. If you're flipping bits, use&. They're cousins, not twins!" - Interview Tip: Bitwise operators are frequently tested in coding interviews for FAANG companies, embedded systems, game development, and performance-critical applications worldwide. This pattern appears in interviews across the US, Europe, Asia, and Australia.
- Real Project: Bitwise operators are used in device drivers (everywhere!), encryption algorithms (global!), graphics programming (games!), and networking protocols (worldwide!).
What are bitwise operators in C?
Bitwise operators in C are special operators that manipulate data at the binary bit level. They include AND (&), OR (|), XOR (^), left shift (<<), right shift (>>), and one's complement (~). These operators are faster than arithmetic operations and are directly supported by the processor — the same in any C compiler, anywhere in the world.
1. Bitwise AND Operator (&)
Imagine you're checking if two light switches are both ON. That's exactly what the & operator does — it checks bit by bit, and only says "YES" (1) when BOTH bits are 1. If either is 0, it says "NO" (0). Simple, right?
The bitwise AND operator is denoted by the single ampersand sign (&). Two integer operands are written on both sides of the operator. If the corresponding bits of both operands are 1, then the output is 1; otherwise, the output is 0.
This concept is used globally — from controlling LEDs on an Arduino (US) to building device drivers in Germany to optimizing game engines in Japan. Once you master it, you can work anywhere.
Truth Table for Bitwise Operators
| a | b | a & b | a | b | a ^ b |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Example: We have two variables a = 5 and b = 3.
- a = 0101 (binary)
- b = 0011 (binary)
- a & b = 0001 = 1 (decimal)
Result: 5 & 3 = 1
C Program to Demonstrate Bitwise AND Operator
#include <stdio.h>
int main()
{
int a = 5, b = 3, c;
c = a & b;
printf("The output of AND operator a&b is %d", c);
return 0;
}
Output: The output of AND operator a&b is 1
Explanation: 0101 & 0011 = 0001 = 1
2. Bitwise OR Operator (|)
The bitwise OR operator is represented by a single vertical sign (|). If the bit value of either operand is 1, then the output is 1; otherwise, it's 0. This operator returns 1 when at least one bit is 1.
Example: If c = 5 and d = 2, then c | d = 7.
C Program to Demonstrate Bitwise OR Operator
#include <stdio.h>
int main()
{
int a = 5, b = 3, c;
c = a | b;
printf("The output of OR operator a|b is %d", c);
return 0;
}
Output: The output of OR operator a|b is 7
Explanation: 0101 | 0011 = 0111 = 7
3. Bitwise XOR Operator (^)
The bitwise XOR (exclusive OR) operator is denoted by ^. It returns 1 if the bits in the operands are different, and 0 if they're the same.
Personally, I think XOR is the most underrated operator. It's incredibly useful for swapping values without a temporary variable, encrypting data, and finding unique elements in arrays — algorithms used by developers from Silicon Valley to Singapore.
Example: 10 = 00001010 (binary), 12 = 00001100 (binary)
- 00001010 ^ 00001100 = 00000110 = 6 (decimal)
C Program to Demonstrate Bitwise XOR Operator
#include <stdio.h>
int main()
{
int a = 10, b = 12;
printf("XOR result: %d", (a ^ b));
return 0;
}
Output: XOR result: 6
4. Bitwise Left Shift Operator (<<)
The left shift operator shifts all bits towards the left by a specified number of positions. Vacated positions on the right are filled with 0. Each left shift doubles the value — this is a global optimization used by programmers everywhere.
Syntax: operand << n
Where n is the number of bits to shift.
Example: 5 << 2 = 20
- 5 = 0101 (binary)
- 0101 << 2 = 00010100 = 20 (decimal)
5. Bitwise Right Shift Operator (>>)
The right shift operator shifts all bits towards the right by a specified number of positions. Each right shift halves the value (ignoring the fractional part).
Example: 5 >> 1 = 2
- 5 = 0101 (binary)
- 0101 >> 1 = 0010 = 2 (decimal)
6. One's Complement Operator (~)
The one's complement operator is a unary operator that flips all bits: 1 becomes 0, and 0 becomes 1. For any integer n, ~n = -(n+1) due to two's complement representation.
Example: 35 = 00100011 (binary)
- ~00100011 = 11011100 = -36 (decimal)
Important: The bitwise complement of 35 is -36, not 220, because C uses two's complement representation — the same on any system worldwide.
C Program to Demonstrate All Bitwise Operators
#include <stdio.h>
int main()
{
unsigned char a = 5, b = 9;
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a & b);
printf("a|b = %d\n", a | b);
printf("a^b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("b<<1 = %d\n", b << 1);
printf("b>>1 = %d\n", b >> 1);
return 0;
}
Output:
a = 5
b = 9
a&b = 1
a|b = 13
a^b = 12
~a = -6
b<<1 = 18
b>>1 = 4
🤔 Complete FAQ - Supporting Questions
Primary Question: "What are bitwise operators in C?"
🔹 Follow-up Question 1: Why are bitwise operators faster than arithmetic operators?
Bitwise operations are directly supported by the CPU's arithmetic logic unit (ALU) and execute in a single clock cycle — whether you're running on an Intel in the US, an ARM in the UK, or a RISC-V in India.
🔹 Follow-up Question 2: Can I use bitwise operators on floating-point numbers?
No, bitwise operators in C only work on integer types (int, char, short, long). They cannot be applied to float or double — this is true for every C standard worldwide.
🔹 Follow-up Question 3: What's the practical use of bitwise operators?
Bitwise operators are used in device drivers (everywhere!), encryption algorithms (global!), graphics programming (games worldwide!), compression algorithms (industry-standard!), and low-level system programming across all platforms.
🔹 Follow-up Question 4: What's the difference between & and && in C?
& is the bitwise AND operator that works on individual bits. && is the logical AND operator that works on boolean values (0 or non-zero). This distinction is the same in any C compiler — GCC, Clang, or MSVC.
🔹 Follow-up Question 5: How does the complement operator work in two's complement?
In two's complement, ~n = -(n+1). For example, ~5 = -6. This is how C represents negative numbers — a universal standard in computing worldwide.
🔹 Follow-up Question 6: Which bitwise operator is used to check if a number is even or odd?
Use n & 1. If the result is 1, the number is odd. If 0, the number is even. This is faster than using n % 2 and is used in performance-critical code globally.
🚀 Practice Questions
Ready to test your skills? Try these practice problems — they're favorites in coding interviews across the US, Europe, and Asia:
- Write a program to check if a number is even or odd using bitwise AND (FAANG favorite!)
- Write a program to swap two numbers using XOR (global interview question)
- Write a program to count the number of 1s in a binary representation
- Write a program to multiply a number by 4 using left shift
- Write a program to divide a number by 2 using right shift
Hint: Use n & 1 to check odd/even, and a ^= b; b ^= a; a ^= b; for XOR swap. These patterns work everywhere — from Silicon Valley to Sydney.