Open In App

Check whether K-th bit is set or not

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number n and a bit position k, check if the kth bit of n is set or not. A bit is called set if it is 1. 

Note: Indexing starts with 0 from LSB (least significant bit) side in the binary representation of the number.

Examples: 

Input: n = 7, k = 2
Output: Yes
Explanation: 7 is represented as 111 in binary and bit at position 2 is set.

Input: n = 5, k = 1
Output: No
Explanation: 5 is represented as 101 in binary and bit at position 1 is not set.

Using Left Shift Operator - O(k) time and O(1) space

The idea is to leverage bitwise operations to check if a specific bit is set. Create a number that has only the k-th bit set (by left-shifting 1 by k positions). Then we perform a bitwise AND operation between this number and the given number n. If the result is non-zero, it means the k-th bit in n is set to 1, otherwise it's 0.

C++
// CPP program to check if k-th bit
// of a given number is set or not
#include <iostream>
using namespace std;

bool checkKthBit(int n, int k) {
    
    // Value whose only kth bit 
    // is set. 
    int val = (1<<k);
    
    // If AND operation of n and 
    // value is non-zero, it means
    // k'th bit is set 
    if ((n&val) != 0) {
        return true;
    } 
    
    return false;
}

int main() {
    int n = 7, k = 2;

    if (checkKthBit(n, k)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}
Java
// Java program to check if k-th bit
// of a given number is set or not
class GfG {

    static boolean checkKthBit(int n, int k) {
        
        // Value whose only kth bit 
        // is set. 
        int val = (1 << k);
        
        // If AND operation of n and 
        // value is non-zero, it means
        // k'th bit is set 
        if ((n & val) != 0) {
            return true;
        } 
        
        return false;
    }

    public static void main(String[] args) {
        int n = 7, k = 2;

        if (checkKthBit(n, k)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}
Python
# Python program to check if k-th bit
# of a given number is set or not

def checkKthBit(n, k):
    
    # Value whose only kth bit 
    # is set. 
    val = (1 << k)
    
    # If AND operation of n and 
    # value is non-zero, it means
    # k'th bit is set 
    if (n & val) != 0:
        return True
    
    return False

if __name__ == "__main__":
    n, k = 7, 2

    if checkKthBit(n, k):
        print("Yes")
    else:
        print("No")
C#
// C# program to check if k-th bit
// of a given number is set or not
using System;

class GfG {

    static bool checkKthBit(int n, int k) {
        
        // Value whose only kth bit 
        // is set. 
        int val = (1 << k);
        
        // If AND operation of n and 
        // value is non-zero, it means
        // k'th bit is set 
        if ((n & val) != 0) {
            return true;
        } 
        
        return false;
    }

    static void Main() {
        int n = 7, k = 2;

        if (checkKthBit(n, k)) {
            Console.WriteLine("Yes");
        } else {
            Console.WriteLine("No");
        }
    }
}
JavaScript
// JavaScript program to check if k-th bit
// of a given number is set or not

function checkKthBit(n, k) {
    
    // Value whose only kth bit 
    // is set. 
    let val = (1 << k);
    
    // If AND operation of n and 
    // value is non-zero, it means
    // k'th bit is set 
    if ((n & val) !== 0) {
        return true;
    } 
    
    return false;
}

let n = 7, k = 2;

if (checkKthBit(n, k)) {
    console.log("Yes");
} else {
    console.log("No");
}

Output
Yes

Using Right Shift Operator - O(k) time and O(1) space

The idea is to shift the bits of the given number to the right by k positions, which brings the k-th bit to the rightmost position (0th position). Then we perform a bitwise AND operation with 1. If this result is non-zero (meaning the bit is 1), then the k-th bit in the original number was set, otherwise it wasn't.

C++
// CPP program to check if k-th bit
// of a given number is set or not
#include <iostream>
using namespace std;

bool checkKthBit(int n, int k) {
    
    // Right shift n by k 
    n = n >> k;
    
    // If 0th bit is set 
    if ((n & 1) != 0) {
        return true;
    }
    
    return false;
}

int main() {
    int n = 7, k = 2;

    if (checkKthBit(n, k)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}
Java
// Java program to check if k-th bit
// of a given number is set or not
class GfG {

    static boolean checkKthBit(int n, int k) {
        
        // Right shift n by k 
        n = n >> k;
        
        // If 0th bit is set 
        if ((n & 1) != 0) {
            return true;
        }
        
        return false;
    }

    public static void main(String[] args) {
        int n = 7, k = 2;

        if (checkKthBit(n, k)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}
Python
# Python program to check if k-th bit
# of a given number is set or not

def checkKthBit(n, k):
    
    # Right shift n by k 
    n = n >> k
    
    # If 0th bit is set 
    if (n & 1) != 0:
        return True
    
    return False

if __name__ == "__main__":
    n, k = 7, 2

    if checkKthBit(n, k):
        print("Yes")
    else:
        print("No")
C#
// C# program to check if k-th bit
// of a given number is set or not
using System;

class GfG {

    static bool checkKthBit(int n, int k) {
        
        // Right shift n by k 
        n = n >> k;
        
        // If 0th bit is set 
        if ((n & 1) != 0) {
            return true;
        }
        
        return false;
    }

    static void Main() {
        int n = 7, k = 2;

        if (checkKthBit(n, k)) {
            Console.WriteLine("Yes");
        } else {
            Console.WriteLine("No");
        }
    }
}
JavaScript
// JavaScript program to check if k-th bit
// of a given number is set or not

function checkKthBit(n, k) {
    
    // Right shift n by k 
    n = n >> k;
    
    // If 0th bit is set 
    if ((n & 1) !== 0) {
        return true;
    }
    
    return false;
}

let n = 7, k = 2;

if (checkKthBit(n, k)) {
    console.log("Yes");
} else {
    console.log("No");
}

Output
Yes

Check whether K-th bit is set or not | DSA Problem
Article Tags :

Similar Reads