Check if i-th Bit is Set in a Number - Visualization

Visualization Player

Problem Statement

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.

Examples

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

Solution

Understanding the Problem

We are given two integers:

  • n – a non-negative integer whose binary representation we need to inspect
  • i – 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 be false.
  • Case 3: n is 0 – All bits are unset. No matter what i is, the result is always false.
  • Case 4: i is negative – Should be considered invalid input. In real implementations, we check if (i < 0) and handle accordingly.

Algorithm Steps

  1. Create a bitmask by left-shifting 1 by i positions: mask = 1 << i.
  2. Use bitwise AND between n and mask: result = n & mask.
  3. If result != 0, it means the i-th bit is set. Otherwise, it's not set.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#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;
}

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(1)Checking whether the i-th bit is set involves a constant-time bitwise operation, regardless of the values of n or i.
Average CaseO(1)Bitwise shift and AND operations take constant time on modern CPUs, so the time complexity remains constant on average.
Worst CaseO(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)

Explanation: The algorithm uses only a fixed number of variables for the mask and result, so space usage remains constant regardless of input size.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...