Check if K-th Bit is Set

ArpitAcharya
ArpitAcharya

Code

#include <stdio.h>

int isKthBitSet(int n, int k) {
    // Write your code here
    return (n>>k) & 1;
    
}

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    printf("%d", isKthBitSet(n, k));
    return 0;
}

Solving Approach

 

The idea is to shift the kth bit to the LSB and then AND with it 1, to check if the resultant is 0 or 1;
if the result is 1(its been ANDed with 1, the original kth bit) and opposite for the kth bit being 0;

now optimized!

Loading...

Input

8 3

Expected Output

1