Check if i-th Bit is Set
Problem and examples
Given a non-negative integer n and an integer i, your task is to determine whether the i-th bit in the binary representation of n is set (i.e., equal to 1).
Bit positions are considered from right to left starting from 0 (0-based indexing).
Return true if the i-th bit is set, otherwise return false.
| n | i | Output | Description |
|---|---|---|---|
| 5 | 0 | true | Binary of 5 is 101; 0-th bit (rightmost) is 1 |
| 5 | 1 | false | Binary of 5 is 101; 1st bit is 0 |
| 5 | 2 | true | Binary of 5 is 101; 2nd bit is 1 |
| 0 | 0 | false | Binary of 0 is 000; no bit is set |
| 1 | 5 | false | Binary of 1 is 000001; 5th bit is 0 |
| 255 | 7 | true | Binary of 255 is 11111111; all lower 8 bits set, including 7th |
| 1024 | 10 | true | Binary of 1024 is 10000000000; 10th bit is 1 |
| 1024 | 9 | false | Binary of 1024 is 10000000000; 9th bit is 0 |
| 0 | 100 | false | Even for large i, all bits in 0 are 0 |
| "" | 0 | false | Empty input for n is treated as 0 |
Explore Check if i-th Bit is Set in a Number
Load an example or enter your own input. Use the player controls to follow each change.
Understanding the Problem
We are given two integers:
n– a non-negative integer whose binary representation we need to inspecti– the position (0-based) of the bit we want to check
The task is to determine whether the i-th bit in the binary form of n is set (equal to 1). Bit positions start from the rightmost bit as position 0.
If the i-th bit is set, return true. Otherwise, return false.
Step-by-Step Solution with Example
step 1: Understand the binary representation of the number
Let’s take an example: n = 13 and i = 2.
Binary representation of 13 is 1101 (from right to left, bits are at positions 3, 2, 1, 0).
So, bits are: [1][1][0][1]
Now we want to check if the 2nd bit (from right) is set (i.e., is it 1?).
step 2: Create a bitmask for the i-th bit
We left-shift 1 by i positions to create a mask.
int mask = 1 << i; // i = 2 → mask = 1 << 2 = 100 in binary = 4
step 3: Perform bitwise AND with the number
Now, use bitwise AND between n and mask:
int result = n & mask; // 13 & 4 = 1101 & 0100 = 0100 = 4
If result ≠ 0, then the bit is set.
step 4: Return the final answer
return result != 0; // true (since bit is set)
Therefore, the output is true, meaning the 2nd bit in 13 is set.
Edge Cases
- Case 1: i is 0 – You are checking the rightmost bit. Works the same way.
- Case 2: i is larger than number of bits in n – For example,
n = 3(binary 11),i = 4. The result will be 0 because that bit does not exist, so answer will befalse. - Case 3: n is 0 – All bits are unset. No matter what
iis, the result is alwaysfalse. - Case 4: i is negative – Should be considered invalid input. In real implementations, we check
if (i < 0)and handle accordingly.
- Create a bitmask by left-shifting 1 by
ipositions:mask = 1 << i. - Use bitwise AND between
nandmask:result = n & mask. - If
result != 0, it means the i-th bit is set. Otherwise, it's not set.
Implementation and efficiency
C implementation
#include <stdio.h>
#include <stdbool.h>
bool isIthBitSet(unsigned int n, unsigned int i) {
unsigned int mask = 1u << i;
return (n & mask) != 0;
}
int main() {
unsigned int number = 37; // binary 100101
unsigned int bitPosition = 3;
if (isIthBitSet(number, bitPosition)) {
printf("Bit %u is set in %u.\n", bitPosition, number);
} else {
printf("Bit %u is not set in %u.\n", bitPosition, number);
}
return 0;
}C++ implementation
#include <iostream>
using namespace std;
bool isIthBitSet(unsigned int n, unsigned int i) {
unsigned int mask = 1u << i;
return (n & mask) != 0;
}
int main() {
unsigned int number = 37; // binary 100101
unsigned int bitPosition = 3;
if (isIthBitSet(number, bitPosition)) {
cout << "Bit " << bitPosition << " is set in " << number << ".\n";
} else {
cout << "Bit " << bitPosition << " is not set in " << number << ".\n";
}
return 0;
}Python implementation
def is_ith_bit_set(n: int, i: int) -> bool:
mask = 1 << i
return (n & mask) != 0
number = 37 # binary 100101
bit_position = 3
if is_ith_bit_set(number, bit_position):
print(f"Bit {bit_position} is set in {number}.")
else:
print(f"Bit {bit_position} is not set in {number}.")Java implementation
public class CheckIthBit {
public static boolean isIthBitSet(int n, int i) {
int mask = 1 << i;
return (n & mask) != 0;
}
public static void main(String[] args) {
int number = 37; // binary 100101
int bitPosition = 3;
if (isIthBitSet(number, bitPosition)) {
System.out.println("Bit " + bitPosition + " is set in " + number + ".");
} else {
System.out.println("Bit " + bitPosition + " is not set in " + number + ".");
}
}
}JavaScript implementation
function isIthBitSet(n, i) {
const mask = 1 << i;
return (n & mask) !== 0;
}
const number = 37; // binary 100101
const bitPosition = 3;
if (isIthBitSet(number, bitPosition)) {
console.log(`Bit ${bitPosition} is set in ${number}.`);
} else {
console.log(`Bit ${bitPosition} is not set in ${number}.`);
}Go implementation
package main
import "fmt"
func isIthBitSet(n uint, i uint) bool {
mask := uint(1) << i
return (n & mask) != 0
}
func main() {
number := uint(37) // binary 100101
bitPosition := uint(3)
if isIthBitSet(number, bitPosition) {
fmt.Printf("Bit %d is set in %d.\n", bitPosition, number)
} else {
fmt.Printf("Bit %d is not set in %d.\n", bitPosition, number)
}
}Rust implementation
fn is_ith_bit_set(n: u32, i: u32) -> bool {
let mask = 1 << i;
(n & mask) != 0
}
fn main() {
let number = 37; // binary 100101
let bit_position = 3;
if is_ith_bit_set(number, bit_position) {
println!("Bit {} is set in {}.", bit_position, number);
} else {
println!("Bit {} is not set in {}.", bit_position, number);
}
}Kotlin implementation
fun isIthBitSet(n: Int, i: Int): Boolean {
val mask = 1 shl i
return (n and mask) != 0
}
fun main() {
val number = 37 // binary 100101
val bitPosition = 3
if (isIthBitSet(number, bitPosition)) {
println("Bit $bitPosition is set in $number.")
} else {
println("Bit $bitPosition is not set in $number.")
}
}Swift implementation
func isIthBitSet(_ n: UInt, _ i: UInt) -> Bool {
let mask = 1 << i
return (n & mask) != 0
}
let number: UInt = 37 // binary 100101
let bitPosition: UInt = 3
if isIthBitSet(number, bitPosition) {
print("Bit \(bitPosition) is set in \(number).")
} else {
print("Bit \(bitPosition) is not set in \(number).")
}TypeScript implementation
function isIthBitSet(n: number, i: number): boolean {
const mask = 1 << i;
return (n & mask) !== 0;
}
const number = 37; // binary 100101
const bitPosition = 3;
if (isIthBitSet(number, bitPosition)) {
console.log(`Bit ${bitPosition} is set in ${number}.`);
} else {
console.log(`Bit ${bitPosition} is not set in ${number}.`);
}Time complexity
| Case | Time | Explanation |
|---|---|---|
| best case | O(1) | Checking whether the i-th bit is set involves a constant-time bitwise operation, regardless of the values of n or i. |
| average case | O(1) | Bitwise shift and AND operations take constant time on modern CPUs, so the time complexity remains constant on average. |
| worst case | O(1) | Even in the worst case (e.g., checking very high bit positions), the operation is performed in constant time using bitwise operations. |
Space complexity
O(1)
The algorithm uses only a fixed number of variables for the mask and result, so space usage remains constant regardless of input size.