Bitwise operators are special operators in C Programming that perform operations directly on the binary representation of numbers. Unlike arithmetic operators, bitwise operators manipulate individual bits, making them extremely fast and memory-efficient.
Bitwise operations are widely used in:
- Embedded Systems
- Device Drivers
- Operating Systems
- Networking
- Cryptography
- Data Compression
- Graphics Programming
- Competitive Programming
The six primary bitwise operators in C are:
| Operator | Description |
|---|---|
& | Bitwise AND |
| ` | ` |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left Shift |
>> | Right Shift |
Understanding these operators helps programmers write optimized code and solve low-level programming problems efficiently.
In this chapter, you’ll practice the most important bitwise operator programs with complete solutions, sample outputs, explanations, and concepts covered. C Bitwise Operators practice questions with solutions help to build concepts.
1. C Program to Perform Bitwise AND Operation
Problem Statement
Write a C program to perform the Bitwise AND operation on two numbers.
C Solution
#include <stdio.h>
int main()
{
int firstNumber = 12;
int secondNumber = 10;
printf("Bitwise AND = %d",
firstNumber & secondNumber);
return 0;
}
Sample Output
Bitwise AND = 8
Explanation
Binary Representation:
12 = 1100
10 = 1010
------------
AND=1000 = 8
The Bitwise AND operator returns 1 only if both corresponding bits are 1.
Concepts Covered
- Bitwise AND
- Binary Numbers
- Bit Manipulation
2. C Program to Perform Bitwise OR Operation
Problem Statement
Write a C program to perform the Bitwise OR operation.
C Solution
#include <stdio.h>
int main()
{
int firstNumber = 12;
int secondNumber = 10;
printf("Bitwise OR = %d",
firstNumber | secondNumber);
return 0;
}
Sample Output
Bitwise OR = 14
Explanation
Binary Representation:
12 = 1100
10 = 1010
------------
OR =1110 =14
The OR operator returns 1 if at least one bit is 1.
Concepts Covered
- Bitwise OR
- Binary Operations
- Bit Manipulation
3. C Program to Perform Bitwise XOR Operation
Problem Statement
Write a C program to perform the Bitwise XOR operation.
C Solution
#include <stdio.h>
int main()
{
int firstNumber = 12;
int secondNumber = 10;
printf("Bitwise XOR = %d",
firstNumber ^ secondNumber);
return 0;
}
Sample Output
Bitwise XOR = 6
Explanation
Binary Representation:
12 =1100
10 =1010
-----------
XOR=0110 =6
The XOR operator returns 1 only when the corresponding bits are different.
Concepts Covered
- Bitwise XOR
- Binary Numbers
- Bit Manipulation
4. C Program to Perform Bitwise NOT Operation
Problem Statement
Write a C program to perform the Bitwise NOT operation on a number.
C Solution
#include <stdio.h>
int main()
{
int number = 12;
printf("Bitwise NOT = %d", ~number);
return 0;
}
Sample Output
Bitwise NOT = -13
Explanation
The Bitwise NOT (~) operator flips every bit of the number.
Binary Representation:
12 = 00001100
~12 = 11110011
Since C uses 2’s Complement representation for signed integers, the result becomes -13.
Concepts Covered
- Bitwise NOT
- One’s Complement
- Two’s Complement
- Binary Numbers
5. C Program to Perform Left Shift Operation
Problem Statement
Write a C program to perform the Left Shift (<<) operation.
C Solution
#include <stdio.h>
int main()
{
int number = 5;
printf("Left Shift by 1 = %d", number << 1);
return 0;
}
Sample Output
Left Shift by 1 = 10
Explanation
Binary Representation:
5 = 00000101
5 << 1
= 00001010
= 10
Each left shift multiplies the number by 2.
| Expression | Result |
|---|---|
5 << 1 | 10 |
5 << 2 | 20 |
5 << 3 | 40 |
Concepts Covered
- Left Shift
- Bit Manipulation
- Binary Numbers
- Multiplication Using Bits
6. C Program to Perform Right Shift Operation
Problem Statement
Write a C program to perform the Right Shift (>>) operation.
C Solution
#include <stdio.h>
int main()
{
int number = 20;
printf("Right Shift by 2 = %d", number >> 2);
return 0;
}
Sample Output
Right Shift by 2 = 5
Explanation
Binary Representation:
20 = 00010100
20 >> 2
= 00000101
= 5
Every right shift divides the number by 2 (for positive integers).
| Expression | Result |
|---|---|
20 >> 1 | 10 |
20 >> 2 | 5 |
20 >> 3 | 2 |
Concepts Covered
- Right Shift
- Binary Numbers
- Division Using Bits
- Bit Manipulation
7. C Program to Check Whether a Number is Even or Odd Using Bitwise AND
Problem Statement
Write a C program to determine whether a number is even or odd using the Bitwise AND operator.
C Solution
#include <stdio.h>
int main()
{
int number = 27;
if(number & 1)
{
printf("Odd Number");
}
else
{
printf("Even Number");
}
return 0;
}
Sample Output
Odd Number
Explanation
The least significant bit determines whether a number is even or odd.
- Last bit = 1 → Odd Number
- Last bit = 0 → Even Number
Example:
27 = 11011
11011
00001
-----
00001
Since the result is 1, the number is odd.
Concepts Covered
- Bitwise AND
- Even/Odd Detection
- Binary Numbers
- Conditional Statements
8. C Program to Swap Two Numbers Using Bitwise XOR
Problem Statement
Write a C program to swap two numbers using the Bitwise XOR operator without using a third variable.
C Solution
#include <stdio.h>
int main()
{
int firstNumber = 15;
int secondNumber = 20;
printf("Before Swap:\n");
printf("%d %d\n", firstNumber, secondNumber);
firstNumber = firstNumber ^ secondNumber;
secondNumber = firstNumber ^ secondNumber;
firstNumber = firstNumber ^ secondNumber;
printf("After Swap:\n");
printf("%d %d", firstNumber, secondNumber);
return 0;
}
Sample Output
Before Swap:
15 20
After Swap:
20 15
Explanation
The XOR operator allows two numbers to be swapped without using an extra variable.
Concepts Covered
- Bitwise XOR
- Swapping
- Bit Manipulation
- Optimization
9. C Program to Check Whether the Nth Bit is Set or Not
Problem Statement
Write a C program to check whether the Nth bit of a number is set.
C Solution
#include <stdio.h>
int main()
{
int number = 10;
int position = 2;
if(number & (1 << position))
{
printf("Bit is Set");
}
else
{
printf("Bit is Not Set");
}
return 0;
}
Sample Output
Bit is Not Set
Explanation
The expression:
1 << position
creates a mask with only the specified bit set. Using Bitwise AND determines whether that bit is set in the original number.
Concepts Covered
- Left Shift
- Bit Masking
- Bitwise AND
- Bit Checking
10. C Program to Set the Nth Bit of a Number
Problem Statement
Write a C program to set the Nth bit of a number.
C Solution
#include <stdio.h>
int main()
{
int number = 10;
int position = 1;
number = number | (1 << position);
printf("Updated Number = %d", number);
return 0;
}
Sample Output
Updated Number = 10
Note: In this example, the specified bit is already set, so the value remains unchanged.
Explanation
The Bitwise OR operator sets the required bit without affecting the remaining bits.
Example:
10 = 1010
Mask = 0010
1010
0010
----
1010
Concepts Covered
- Bitwise OR
- Bit Masking
- Left Shift
- Bit Manipulation
11. C Program to Clear the Nth Bit
Problem Statement
Write a C program to clear the Nth bit of a number.
C Solution
#include <stdio.h>
int main()
{
int number = 15;
int position = 2;
number = number & ~(1 << position);
printf("Updated Number = %d", number);
return 0;
}
Sample Output
Updated Number = 11
Explanation
The expression:
~(1 << position)
creates a mask with every bit set except the specified bit. Applying the Bitwise AND operator clears only that bit while leaving all other bits unchanged.
Binary Representation:
15 = 1111
Mask = 1011
1111
1011
----
1011 = 11
Concepts Covered
- Bitwise AND
- Bitwise NOT
- Bit Masking
- Bit Manipulation
12. C Program to Toggle the Nth Bit
Problem Statement
Write a C program to toggle (flip) the Nth bit of a number.
C Solution
#include <stdio.h>
int main()
{
int number = 10;
int position = 1;
number = number ^ (1 << position);
printf("Updated Number = %d", number);
return 0;
}
Sample Output
Updated Number = 8
Explanation
The Bitwise XOR operator flips the specified bit.
- If the bit is 1, it becomes 0.
- If the bit is 0, it becomes 1.
Binary Representation:
10 = 1010
Mask = 0010
1010
0010
----
1000 = 8
Concepts Covered
- Bitwise XOR
- Toggle Bit
- Bit Masking
- Bit Manipulation
13. C Program to Count the Number of Set Bits
Problem Statement
Write a C program to count the number of set bits (1s) in a number.
C Solution
#include <stdio.h>
int main()
{
int number = 13;
int count = 0;
while(number > 0)
{
if(number & 1)
{
count++;
}
number = number >> 1;
}
printf("Total Set Bits = %d", count);
return 0;
}
Sample Output
Total Set Bits = 3
Explanation
Binary Representation:
13 = 1101
There are three 1s, so the output is 3.
Concepts Covered
- Bitwise AND
- Right Shift
- Bit Counting
- Loops
14. C Program to Check Whether a Number is a Power of Two
Problem Statement
Write a C program to determine whether a number is a power of two.
C Solution
#include <stdio.h>
int main()
{
int number = 16;
if(number > 0 && (number & (number - 1)) == 0)
{
printf("Power of Two");
}
else
{
printf("Not a Power of Two");
}
return 0;
}
Sample Output
Power of Two
Explanation
A power of two has exactly one set bit.
Examples:
2 = 0010
4 = 0100
8 = 1000
16 =10000
For such numbers:
n & (n - 1)
always evaluates to 0.
Concepts Covered
- Bitwise AND
- Power of Two
- Binary Numbers
- Optimization
15. C Program to Demonstrate All Bitwise Operators
Problem Statement
Write a C program demonstrating all six Bitwise Operators.
C Solution
#include <stdio.h>
int main()
{
int firstNumber = 12;
int secondNumber = 10;
printf("AND = %d\n", firstNumber & secondNumber);
printf("OR = %d\n", firstNumber | secondNumber);
printf("XOR = %d\n", firstNumber ^ secondNumber);
printf("NOT = %d\n", ~firstNumber);
printf("LEFT = %d\n", firstNumber << 1);
printf("RIGHT= %d\n", firstNumber >> 1);
return 0;
}
Sample Output
AND = 8
OR = 14
XOR = 6
NOT = -13
LEFT = 24
RIGHT= 6
Explanation
This program demonstrates the behavior of all commonly used Bitwise Operators in C.
Concepts Covered
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- Bitwise NOT
- Left Shift
- Right Shift
Chapter Summary
In this chapter, you learned how Bitwise Operators manipulate individual bits to perform efficient low-level operations. You practiced Bitwise AND, OR, XOR, NOT, Left Shift, Right Shift, checking even/odd numbers, swapping values without a temporary variable, setting, clearing, toggling bits, counting set bits, and determining whether a number is a power of two. These concepts are widely used in embedded systems, operating systems, networking, device drivers, graphics programming, and competitive programming.
Key Takeaways
- Bitwise operators work directly on binary values.
&performs Bitwise AND.|performs Bitwise OR.^performs Bitwise XOR.~performs Bitwise NOT.<<shifts bits to the left (generally multiplies by 2).>>shifts bits to the right (generally divides by 2).- Bit masking helps set, clear, toggle, and test individual bits.
- Bitwise operations are extremely fast and memory-efficient.
- These operators are heavily used in system-level programming.
Frequently Asked Questions (FAQs)
1. What are Bitwise Operators in C?
Bitwise operators manipulate the binary representation of integers by operating directly on individual bits.
2. Which Bitwise Operators are available in C?
C provides six primary bitwise operators:
&(AND)|(OR)^(XOR)~(NOT)<<(Left Shift)>>(Right Shift)
3. What is Bit Masking?
Bit masking is the technique of using bitwise operations with masks to set, clear, toggle, or check specific bits in a number.
4. Why are Bitwise Operators faster than arithmetic operations?
They work directly on bits at the hardware level, often requiring fewer CPU instructions than equivalent arithmetic operations.
5. Where are Bitwise Operators used?
They are commonly used in embedded systems, operating systems, networking, graphics programming, cryptography, compression algorithms, and hardware control.
6. How can you check if a number is even using Bitwise Operators?
Use:
number & 1
If the result is 0, the number is even; otherwise, it is odd.
7. How do you determine whether a number is a power of two?
A positive number is a power of two if:
(number & (number - 1)) == 0
8. Why are Bitwise Operators frequently asked in interviews?
They test your understanding of binary representation, low-level programming, optimization techniques, bit manipulation, and algorithmic thinking, making them a common topic in technical interviews and competitive programming.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
