Home › C Tutorials › Operators › Bitwise Operators
Bitwise Operators in C: AND, OR, XOR, Shift & Complement
Learn all six bitwise operators with truth tables, code examples, and clear explanations
📚 Part of the Operators in C series. New to operators? Start with: Operators and Expressions in C
Last Updated: October 5, 2026
About the Author
Sankalan Data Technologies
Programming & Data Analytics Tutorials
Published by Sankalan Data Technologies. Our tutorials and data guides are independently developed and curated to provide practical, hands-on learning resources for Python, SQL, and data analytics practitioners.
Quick Answer: What are Bitwise Operators in C?
Bitwise operators in C perform operations at the binary bit level on integer data.
C has six bitwise operators: AND (&), OR (|),
XOR (^), left shift (<<), right shift (>>),
and complement (~).
These operators are faster than arithmetic operations because the CPU directly supports them.
What You'll Learn in This Tutorial
- Bitwise AND Operator (&)
- Bitwise OR Operator (|)
- Bitwise XOR Operator (^)
- Left Shift and Right Shift Operators
- One's Complement Operator (~)
- Practice Questions
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).
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 experience: Many students confuse bitwise AND (&)
with logical AND (&&). I've seen students spend hours debugging a
program, only to realize they used && instead of &.
Here's the rule: if you're checking two conditions, use &&.
If you're flipping bits, use &. They're cousins, not twins.
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) |
1. Bitwise AND Operator (&)
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.
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\n", 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 a = 5 and b = 3, then
a | b = 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\n", 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.
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.
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\n", (a ^ b));
return 0;
}
Output:
XOR result: 6
4 & 5. Left Shift and Right Shift Operators
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.
Syntax: operand << n
Where n is the number of bits to shift.
Example: 5 << 2 = 20
- 5 = 0101 (binary)
- 0101 << 2 = 00010100 = 20 (decimal)
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.
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 |
Complete 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
Common Mistakes to Avoid
- Confusing & with && —
&is bitwise AND,&&is logical AND. They are not the same. - Using bitwise operators on floats — Bitwise operators only work on integer types (int, char, short, long).
- Ignoring operator precedence — Bitwise operators have lower precedence than arithmetic operators.
- Sign bit issues with right shift — Right shift on signed integers may preserve the sign bit on some systems.
- Overflow with left shift — Shifting too far can lose bits or cause undefined behavior.
Practice Questions
Try these practice problems — they're favorites in coding interviews:
- Write a program to check if a number is even or odd using bitwise AND
- Write a program to swap two numbers using XOR
- 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.
Why Bitwise Operators Matter
- Performance: Bitwise operations are up to 10x faster than arithmetic operations
- Hardware Programming: Essential for embedded systems, device drivers, and microcontroller programming
- Career: Essential for FAANG, embedded systems, game dev, and cybersecurity roles
- Security: Used in encryption algorithms, hashing, and checksum calculations
Frequently Asked Questions
What are bitwise operators in C?
Bitwise operators in C work on individual bits of integer data. C has six: AND (&), OR (|), XOR (^), left shift (<<), right shift (>>), and one's complement (~). These operators are directly supported by the CPU, so they're faster than arithmetic operations.
How does the bitwise AND operator work in C?
The bitwise AND operator (&) returns 1 only when both corresponding bits are 1. Otherwise it returns 0. For example, 5 (0101) & 3 (0011) = 1 (0001).
What is the difference between bitwise OR and XOR operators?
Bitwise OR (|) returns 1 if at least one bit is 1. Bitwise XOR (^) returns 1 only if the bits are different. OR is inclusive; XOR is exclusive.
What do left shift and right shift operators do in C?
Left shift (<<) moves bits to the left, filling with zeros, and multiplies by 2 per shift. Right shift (>>) moves bits right, dividing by 2 per shift. Example: 5 << 1 = 10, 5 >> 1 = 2.
What is the one's complement operator in C?
The one's complement operator (~) is a unary operator that flips all bits (0 becomes 1, 1 becomes 0). For any integer n, ~n = -(n+1) due to two's complement representation.
What is 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. & returns an integer result, while && returns either 0 or 1.
Further Reading
Related Tutorials
Previous Topic: Logical Operators in C | Next Topic: Other Operators (?, *, &, sizeof) in C