Python Program to Check if nth Bit is Set
To check if the nth bit is set in Python, use 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
OutputTrue
Inputnum=5, n=1
OutputFalse
Inputnum=8, n=3
OutputTrue
How to Think About It
To find 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; otherwise, it is not.5
Return True if set, False if not.Code
python
def is_nth_bit_set(num, n): return (num & (1 << n)) != 0 # Example usage print(is_nth_bit_set(5, 0)) # True print(is_nth_bit_set(5, 1)) # False print(is_nth_bit_set(8, 3)) # True
Output
True
False
True
Dry Run
Let's trace the example num=5, n=1 through the code
1
Create mask
1 << 1 = 2 (binary 10)
2
Bitwise AND
5 & 2 = 0101 & 0010 = 0000 (decimal 0)
3
Check result
0 != 0 is False, so bit 1 is not set
| Step | Operation | Value |
|---|---|---|
| 1 | Mask (1 << n) | 2 |
| 2 | num & mask | 0 |
| 3 | Result != 0 | False |
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 to isolate that bit.
Step 2: Use bitwise AND
The bitwise AND between the number and the mask keeps only the nth bit of the number; all other bits become zero.
Step 3: Check if bit is set
If the result of the AND is not zero, it means the nth bit was set in the original number.
Alternative Approaches
Using right shift and bitwise AND
python
def is_nth_bit_set(num, n): return ((num >> n) & 1) == 1 print(is_nth_bit_set(5, 0)) # True print(is_nth_bit_set(5, 1)) # False print(is_nth_bit_set(8, 3)) # True
This method shifts the number right by n bits and checks the least significant bit; it is equally efficient and sometimes easier to understand.
Using bin() string and indexing
python
def is_nth_bit_set(num, n): bin_str = bin(num)[2:][::-1] return n < len(bin_str) and bin_str[n] == '1' print(is_nth_bit_set(5, 0)) # True print(is_nth_bit_set(5, 1)) # False print(is_nth_bit_set(8, 3)) # True
This method converts the number to a binary string and checks the character at position n; it is less efficient but useful for learning.
Complexity: O(1) time, O(1) space
Time Complexity
Checking a bit uses simple bitwise operations that run in constant time regardless of input size.
Space Complexity
Only a few variables are used, so space is constant.
Which Approach is Fastest?
Bitwise operations are fastest; string conversion is slower and less memory efficient.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Bitwise AND with mask | O(1) | O(1) | Fastest and simplest |
| Right shift and AND | O(1) | O(1) | Equally fast, easy to read |
| Binary string indexing | O(log n) | O(log n) | Learning and visualization |
Remember bit positions start at 0 from the right (least significant bit).
Beginners often forget that bit positions start at 0, causing off-by-one errors.