Bitwise Operators in C
Inside the computer, every number is stored as bits: zeros and ones. 5 is 101, 3 is 011. Bitwise operators work directly on these bits.
They look difficult, but with small numbers and a truth table they become easy. In this lesson you will learn all six bitwise operators, with every answer worked out in binary.
What You Will Learn
- The 6 bitwise operators:
&,|,^,~,<<,>>. - How to solve them step by step in binary.
- Why left shift multiplies by 2 and right shift divides by 2.
- Checking even/odd with
n & 1. - Swapping two numbers with XOR.
Bitwise Operators in C Explained
Exam definition: Bitwise operators perform operations on the individual bits of integer values. C has six: & (AND), | (OR), ^ (XOR), ~ (NOT / one's complement), << (left shift) and >> (right shift). They work only on integer types.
| Operator | Name | Rule for each bit | Example (5 = 0101, 3 = 0011) | Result |
|---|---|---|---|---|
& | AND | 1 only if both bits are 1 | 5 & 3 = 0001 | 1 |
| | OR | 1 if any bit is 1 | 5 | 3 = 0111 | 7 |
^ | XOR | 1 if the bits are different | 5 ^ 3 = 0110 | 6 |
~ | NOT | Flips every bit | ~5 | −6 |
<< | Left shift | Moves bits left, adds 0s on the right | 5 << 1 = 1010 | 10 |
>> | Right shift | Moves bits right, drops the last bit | 5 >> 1 = 0010 | 2 |
Truth table for one bit
| 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 |
Memory trick: AND = both must agree to say 1. OR = anyone can say 1. XOR = "eXclusive": 1 only when they are different (like a light with two switches). Left shift = × 2, right shift = ÷ 2.
Why is ~5 equal to −6?
~ flips every bit of the whole int (all 32 bits), including the sign bit. In the two's complement system used by computers, ~x is always equal to -(x + 1). So ~5 = −6 and ~0 = −1.
Syntax of Bitwise Operators
a & b a | b a ^ b ~a
a << n /* shift a left by n places = a × 2ⁿ (for non-negative a, if it fits) */
a >> n /* shift a right by n places = a ÷ 2ⁿ (for non-negative a) */
Example 1: All Bitwise Operators
#include <stdio.h>
int main(void)
{
int a = 5, b = 3; /* a = 0101, b = 0011 */
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("a << 1 = %d\n", a << 1);
printf("a >> 1 = %d\n", a >> 1);
printf("1 << 4 = %d\n", 1 << 4);
return 0;
}
Output
a & b = 1 a | b = 7 a ^ b = 6 ~a = -6 a << 1 = 10 a >> 1 = 2 1 << 4 = 16
Code Explanation (Solved in Binary)
0101 (5) 0101 (5) 0101 (5)
& 0011 (3) | 0011 (3) ^ 0011 (3)
---- ---- ----
0001 = 1 0111 = 7 0110 = 6
- AND: only the last column has 1 in both → 0001 = 1.
- OR: every column with at least one 1 → 0111 = 7.
- XOR: columns where the bits differ → 0110 = 6.
- 5 << 1: 0101 → 1010 = 10 (doubled).
- 5 >> 1: 0101 → 0010 = 2 (halved, the last 1 is dropped).
- 1 << 4: 1 moved 4 places = 10000 = 16 = 2⁴.
How Shifting Works
- Each place to the left doubles a binary number, just as each place to the left multiplies by 10 in decimal.
- So
x << n= x × 2ⁿ, andx >> n= x ÷ 2ⁿ (dropping the remainder), for non-negative numbers. - Shifting by a negative number, or by the width of the type or more (32 for
int), is undefined behaviour. Left-shifting a negative number is also undefined.
Example 2: Even/Odd with & and Swap with XOR
#include <stdio.h>
int main(void)
{
int n = 13;
printf("%d is %s\n", n, (n & 1) ? "odd" : "even");
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
x = x ^ y;
y = x ^ y; /* (x ^ y) ^ y = x */
x = x ^ y; /* (x ^ y) ^ x = y */
printf("After swap : x = %d, y = %d\n", x, y);
return 0;
}
Output
13 is odd Before swap: x = 10, y = 20 After swap : x = 20, y = 10
n & 1 keeps only the last bit. Odd numbers end in 1 in binary, even numbers end in 0. The XOR swap works because a ^ b ^ b = a (XOR with the same value twice cancels out).
Important Points to Remember
- 6 bitwise operators:
& | ^ ~ << >>. - They work only on integers.
- XOR gives 1 when bits are different.
~x= −(x + 1).<< 1= × 2;>> 1= ÷ 2.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Using ^ for power: 2^3 | Gives 1 (XOR), not 8 | pow(2, 3) or 1 << 3 |
Using & instead of && in conditions | Bitwise result, no short-circuit | Use && for logic |
n & 1 == 0 for even | == runs first: n & (1 == 0) = 0 | (n & 1) == 0 with brackets |
| Bitwise on a float | Compile error | Use integers only |
Bitwise Operators Practice Questions
- Name the six bitwise operators.
- Find
12 & 10,12 | 10and12 ^ 10. - What is
~10? - What is
3 << 2and20 >> 2? - How do you check if a number is odd using a bitwise operator?
Show answers
&,|,^,~,<<,>>.- 12 = 1100, 10 = 1010. AND = 1000 = 8; OR = 1110 = 14; XOR = 0110 = 6.
- −11 (since
~x= −(x + 1)). 3 << 2= 12 (3 × 4);20 >> 2= 5 (20 ÷ 4).if (n & 1)is true for odd numbers.
Coding Exercise
Read a number and print it multiplied by 8 and divided by 4 using only shift operators.
Show solution
#include <stdio.h>
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("n x 8 = %d\n", n << 3);
printf("n / 4 = %d\n", n >> 2);
return 0;
}
Enter a number: 40 n x 8 = 320 n / 4 = 10
Quick Quiz: Bitwise Operators in C (MCQ)
-
6 & 3equals:A) 2 B) 7 C) 5 D) 1
-
6 | 3equals:A) 2 B) 7 C) 5 D) 9
-
6 ^ 3equals:A) 2 B) 7 C) 5 D) 216
-
~0equals:A) 0 B) 1 C) −1 D) 255
-
1 << 5equals:A) 5 B) 10 C) 16 D) 32
Show answers
- A: 110 & 011 = 010 = 2.
- B: 110 | 011 = 111 = 7.
- C: 110 ^ 011 = 101 = 5.
- C: −1.
- D: 32.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Operators | & | ^ ~ << >> |
|---|---|
| 5 & 3 / 5 | 3 / 5 ^ 3 | 1 / 7 / 6 |
| ~x | −(x + 1); ~5 = −6 |
| x << n | x × 2ⁿ |
| x >> n | x ÷ 2ⁿ |
| Odd check | n & 1 |
| Works on | Integers only |
Frequently asked questions
- What is the difference between & and &&?
&works on each bit;&&works on whole conditions (true/false) with short-circuit.- How do you swap two numbers without a third variable?
- With XOR:
a = a ^ b; b = a ^ b; a = a ^ b; - Why is left shift by 1 the same as multiplying by 2?
- Each binary place is worth twice the one to its right, so moving bits left doubles the value.
- Is ^ the power operator?
- No.
^is XOR; usepow()for powers.
Remember
- XOR = different → 1.
- Left × 2, right ÷ 2; ~x = −(x + 1).
Next Topic
Next lesson: Ternary Operator in C. You will learn a one-line way to choose between two values.