Posts K-th Bit is Set or Not (geeksforgeeks - SDE Sheet)
Post
Cancel

K-th Bit is Set or Not (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given a number n and a bit number k, check if kth index bit of n is set or not. A bit is called set if it is 1. Position of set bit ‘1’ should be indexed starting with 0 from LSB side in binary representation of the number.

geeksforgeeks

SOLUTION

1
2
3
4
5
class CheckBit {
    static boolean checkKthBit(int n, int k) {
        return ( (n&(1<<k)) != 0);
    }
}
This post is licensed under CC BY 4.0 by the author.