C Program to Check if nth Bit is Set
In C, you can check if the nth bit is set by using the expression
(num & (1 << n)) != 0, where num is the number and n is the bit position starting from 0.Examples
Inputnum = 5, n = 0
OutputBit 0 is set
Inputnum = 5, n = 2
OutputBit 2 is set
Inputnum = 8, n = 1
OutputBit 1 is not set
How to Think About It
To check if the nth bit is set, think of shifting the number 1 to the left by n positions to create a mask that has only the nth bit set. Then use the bitwise AND operator
& between the number and this mask. If the result is not zero, the nth bit in the number is set; otherwise, it is not.Algorithm
1
Get the input number and the bit position n.2
Create a mask by shifting 1 left by n positions.3
Perform bitwise AND between the number and the mask.4
If the result is not zero, the nth bit is set; else, it is not.5
Print the result.Code
c
#include <stdio.h> int main() { int num, n; printf("Enter number and bit position: "); scanf("%d %d", &num, &n); if ((num & (1 << n)) != 0) { printf("Bit %d is set\n", n); } else { printf("Bit %d is not set\n", n); } return 0; }
Output
Enter number and bit position: 5 2
Bit 2 is set
Dry Run
Let's trace the input num=5 and n=2 through the code.
1
Input values
num = 5 (binary 0101), n = 2
2
Create mask
1 << 2 = 4 (binary 0100)
3
Bitwise AND
5 & 4 = 4 (binary 0100)
4
Check result
4 != 0, so bit 2 is set
| Operation | Value (decimal) | Value (binary) |
|---|---|---|
| num | 5 | 0101 |
| 1 << n | 4 | 0100 |
| num & (1 << n) | 4 | 0100 |
Why This Works
Step 1: Shift 1 to create mask
Shifting 1 left by n positions creates a number with only the nth bit set, which acts as a mask.
Step 2: Use bitwise AND
The bitwise AND between the number and mask isolates the nth bit of the number.
Step 3: Check if result is zero
If the result is not zero, it means the nth bit was set in the original number.
Alternative Approaches
Using right shift operator
c
#include <stdio.h> int main() { int num, n; printf("Enter number and bit position: "); scanf("%d %d", &num, &n); if (((num >> n) & 1) == 1) { printf("Bit %d is set\n", n); } else { printf("Bit %d is not set\n", n); } return 0; }
This method shifts the number right by n bits and checks the least significant bit; it is equally efficient and sometimes clearer.
Complexity: O(1) time, O(1) space
Time Complexity
The operation uses simple bitwise shifts and AND, which take constant time regardless of input size.
Space Complexity
No extra memory is needed beyond a few variables, so space complexity is constant.
Which Approach is Fastest?
Both left shift with AND and right shift with AND methods run in constant time and are equally fast; choice depends on readability preference.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Left shift and AND | O(1) | O(1) | Clear mask creation |
| Right shift and AND | O(1) | O(1) | Direct bit extraction |
Remember bit positions start at 0 from the right (least significant bit).
Beginners often forget to use parentheses around the shift operation, causing wrong results.