Introduction
Bitwise operators work directly on the individual bits of integer values. They are useful for low-level programming, embedded systems, device control, flags, permissions, and performance-sensitive operations. In this chapter, you will practice the main bitwise operators in C: AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). The examples start with simple binary operations and gradually move toward practical bit manipulation. Bitwise Operators in C Practice questions with solutions to help you understand the concepts.
Q1. Use the Bitwise AND Operator
Problem Statement
Write a C program to perform a bitwise AND operation on two integers.
Use:
12 & 10
C Program
#include <stdio.h>
int main()
{
int a = 12;
int b = 10;
int result;
result = a & b;
printf("Result = %d", result);
return 0;
}
How It Works
The binary values are:
12 = 1100
10 = 1010
----
1000
Binary 1000 is decimal 8.
Sample Output
Result = 8
Concepts Covered
- Bitwise AND
&- Binary representation
- Integer bit manipulation
Q2. Use the Bitwise OR Operator
Problem Statement
Write a C program to perform a bitwise OR operation on 12 and 10.
C Program
#include <stdio.h>
int main()
{
int a = 12;
int b = 10;
int result;
result = a | b;
printf("Result = %d", result);
return 0;
}
How It Works
12 = 1100
10 = 1010
----
1110
Binary 1110 is decimal 14.
Sample Output
Result = 14
Concepts Covered
- Bitwise OR
|- Binary values
- Bit manipulation
Q3. Use the Bitwise XOR Operator
Problem Statement
Write a C program to perform a bitwise XOR operation on 12 and 10.
C Program
#include <stdio.h>
int main()
{
int a = 12;
int b = 10;
int result;
result = a ^ b;
printf("Result = %d", result);
return 0;
}
How It Works
XOR produces 1 when the corresponding bits are different.
12 = 1100
10 = 1010
----
0110
Binary 0110 is decimal 6.
Sample Output
Result = 6
Concepts Covered
- Bitwise XOR
^- Binary comparison
- Bit manipulation
Q4. Use the Bitwise NOT Operator
Problem Statement
Write a C program to apply the bitwise NOT operator to an integer.
C Program
#include <stdio.h>
int main()
{
unsigned int number = 5;
printf("Number = %u\n", number);
printf("After NOT = %u", ~number);
return 0;
}
Sample Output
The exact decimal result depends on the width of unsigned int. On a typical 32-bit unsigned int implementation:
Number = 5
After NOT = 4294967290
How It Works
The bitwise NOT operator ~ flips every bit:
5 = 00000000 00000000 00000000 00000101
~5 = 11111111 11111111 11111111 11111010
Concepts Covered
- Bitwise NOT
~- Bit inversion
unsigned int
Q5. Use the Left Shift Operator
Problem Statement
Write a C program to shift the bits of the number 5 two positions to the left.
C Program
#include <stdio.h>
int main()
{
int number = 5;
int result;
result = number << 2;
printf("Result = %d", result);
return 0;
}
How It Works
5 = 00000101
Shift left by two positions:
00000101 << 2
= 00010100
Binary 00010100 is decimal 20.
Sample Output
Result = 20
Concepts Covered
- Left shift
<<- Bit movement
- Binary representation
Q6. Use the Right Shift Operator
Problem Statement
Write a C program to shift the bits of the number 20 two positions to the right.
C Program
#include <stdio.h>
int main()
{
unsigned int number = 20;
unsigned int result;
result = number >> 2;
printf("Result = %u", result);
return 0;
}
How It Works
20 = 00010100
Shift right by two positions:
00010100 >> 2
= 00000101
Binary 00000101 is decimal 5.
Sample Output
Result = 5
Concepts Covered
- Right shift
>>- Bit movement
- Binary representation
Q7. Check Whether a Number Is Odd or Even Using Bitwise AND
Problem Statement
Write a C program to check whether a number is odd or even using the bitwise AND operator.
C Program
#include <stdio.h>
int main()
{
int number = 27;
if (number & 1)
{
printf("%d is odd.", number);
}
else
{
printf("%d is even.", number);
}
return 0;
}
Sample Output
27 is odd.
How It Works
The last bit of an integer is 1 for odd numbers and 0 for even numbers.
For 27:
27 = 11011
1 = 00001
-----
00001
The result is 1, so the number is odd.
Concepts Covered
- Bitwise AND
&- Odd/even checking
- Binary representation
if-else
Q8. Set a Specific Bit Using Bitwise OR
Problem Statement
Write a C program to set the third bit of a number using the bitwise OR operator.
Consider the bit positions starting from 0 on the right.
C Program
#include <stdio.h>
int main()
{
unsigned int number = 8;
number = number | (1u << 2);
printf("Result = %u", number);
return 0;
}
How It Works
The number is:
8 = 1000
The mask for bit position 2 is:
1 << 2 = 0100
Now:
1000
0100
----
1100
Binary 1100 is decimal 12.
Sample Output
Result = 12
Concepts Covered
- Bitwise OR
|- Left shift
- Bit mask
- Setting a bit
Q9. Clear a Specific Bit Using Bitwise AND and NOT
Problem Statement
Write a C program to clear bit position 2 of the number 12.
C Program
#include <stdio.h>
int main()
{
unsigned int number = 12;
number = number & ~(1u << 2);
printf("Result = %u", number);
return 0;
}
How It Works
Initially:
12 = 1100
Bit position 2 is:
1100
^
Create the mask:
1 << 2 = 0100
Invert it:
~0100
For the relevant four bits:
1011
Then:
1100
1011
----
1000
Binary 1000 is decimal 8.
Sample Output
Result = 8
Concepts Covered
- Bitwise AND
- Bitwise NOT
- Left shift
- Bit mask
- Clearing a bit
Q10. Create a Program to Check, Set, and Clear a Bit
Problem Statement
Create a program that demonstrates three common bit-manipulation operations:
- Check whether bit position
2is set. - Set bit position
2. - Clear bit position
2.
C Program
#include <stdio.h>
int main()
{
unsigned int number = 8;
unsigned int mask = 1u << 2;
printf("Original number = %u\n", number);
if (number & mask)
{
printf("Bit 2 is set.\n");
}
else
{
printf("Bit 2 is not set.\n");
}
number = number | mask;
printf("After setting bit 2 = %u\n", number);
number = number & ~mask;
printf("After clearing bit 2 = %u", number);
return 0;
}
Sample Output
Original number = 8
Bit 2 is set.
After setting bit 2 = 12
After clearing bit 2 = 8
Concepts Covered
- Bitwise AND
- Bitwise OR
- Bitwise NOT
- Left shift
- Bit masks
- Checking a bit
- Setting a bit
- Clearing a bit
Bitwise Operators Quick Reference
| Operator | Name | Example | Purpose |
|---|---|---|---|
& | Bitwise AND | a & b | AND corresponding bits |
| ` | ` | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b | XOR corresponding bits |
~ | Bitwise NOT | ~a | Invert every bit |
<< | Left Shift | a << 2 | Shift bits left |
>> | Right Shift | a >> 2 | Shift bits right |
Bitwise AND, OR and XOR Truth Table
| 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 |
Bit Positions
Bit positions are normally counted from right to left, starting at 0.
For example:
Number = 13
Binary:
1 1 0 1
| | | |
3 2 1 0 ← bit positions
Therefore:
- Bit
0=1 - Bit
1=0 - Bit
2=1 - Bit
3=1
This numbering becomes especially useful when working with bit masks.
Key Takeaways
- Bitwise operators work on individual bits of integer operands mean in c.
&performs bitwise AND.|performs bitwise OR.^performs bitwise XOR.~flips every bit of its operand.<<shifts bits toward higher bit positions.>>shifts bits toward lower bit positions.- Bitwise AND with
1can be used to check the least significant bit. - Bit masks are commonly created using expressions such as
1u << n. - OR can be used to set a bit.
- AND with an inverted mask can be used to clear a bit.
- Bitwise operations in c are different from logical operations such as
&&and||. - Bit shifting should be used carefully because the behavior of some shifts depends on the type and value of the operand.
FAQs
1. What are bitwise operators in C?
Bitwise operators perform operations on the individual bits of integer values.
For example:
int result = 12 & 10;
2. What is the difference between & and &&?
& is the bitwise AND operator and operates on individual bits.
&& is the logical AND operator and evaluates logical conditions.
For example:
5 & 3
performs bitwise manipulation, while:
5 > 2 && 8 > 4
performs a logical operation.
3. What does the XOR operator ^ do?
XOR produces 1 when the corresponding bits are different and 0 when they are the same.
1010
1100
----
0110
4. What does ~ do in C?
The ~ operator performs a bitwise complement. It changes every 0 bit to 1 and every 1 bit to 0.
The exact numeric result depends on the type and its representation.
5. What does << mean in C?
<< is the left-shift operator. It shifts the bits of its left operand toward higher bit positions.
For example:
int result = 5 << 2;
On typical implementations, this produces 20 because the binary representation of 5 is shifted two places to the left.
6. What does >> mean in C?
>> is the right-shift operator. It shifts bits toward lower bit positions. For unsigned integers, zero bits are shifted into the left side.
7. Why are bitwise operators used in C?
Bitwise operators are useful when working with hardware, embedded systems, device registers, bit flags, masks, permissions, and other situations where individual bits need to be manipulated.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
