K-th Bit is Set or Not

Easy Bit Manipulation Basics Original on GeeksforGeeks

K-th Bit is Set or Not is a easy bit manipulation problem solved with the basics pattern. The best approach, optimal (mask with a left shift), runs in O(1) time and O(1) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from divide k times up.

Problem

Check whether the k-th bit (0-indexed from the right) of n is set to 1.

Examples

Example 1

Input
n = 4, k = 2
Output
true
Why
4 = 100 in binary; bit 2 (0-indexed from the right) is 1.

Example 2

Input
n = 4, k = 0
Output
false

Constraints

  • 1 <= n <= 10^9, 0 <= k <= 30; k is 0-indexed from the least significant bit.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Divide k timesO(k)O(1)
Optimal (mask with a left shift)O(1)O(1)

1Divide k times

TimeO(k)
SpaceO(1)

Divide n by 2 k times to bring bit k to the bottom, then check whether it is odd.

  1. Repeat k times: n /= 2. Return n % 2 == 1.
class Solution {
    static boolean checkKthBit(int n, int k) {
        for (int i = 0; i < k; i++) n /= 2;
        return n % 2 == 1;
    }
}

2Optimal (mask with a left shift)

TimeO(1)
SpaceO(1)

1 << k has only bit k set. AND with n keeps just that bit.

  1. return (n & (1 << k)) != 0. (Equivalent: ((n >> k) & 1) == 1.)
class Solution {
    static boolean checkKthBit(int n, int k) {
        return (n & (1 << k)) != 0;
    }
}

Edge cases to test

  • k beyond the highest set bit (false)

Hints

Hint 1

Build a mask with only bit k set: 1 << k. Then n & mask is non-zero exactly when bit k is set.

FAQ

What is the best time complexity for K-th Bit is Set or Not?

Optimal (mask with a left shift) runs in O(1) time and O(1) extra space.

Which pattern does K-th Bit is Set or Not use?

It is a bit manipulation problem that uses the basics pattern. Other problems with the same pattern: Decimal to binary, Convert Binary Number in a Linked List to Integer, Odd or Even.

Is there a brute force solution for K-th Bit is Set or Not?

Yes. Divide k times takes O(k) time and O(1) space. Divide n by 2 k times to bring bit k to the bottom, then check whether it is odd.

Which edge cases should I test for K-th Bit is Set or Not?

k beyond the highest set bit (false).