Check if K-th Bit is Set

Code

#include <stdio.h>

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

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

Solving Approach

 

Right shift the number n by k times and apply 'bit wise and' operation to it. If the result is 1, the bit is set, else the bit is not set.

 

Upvote
Downvote
Loading...

Input

8 3

Expected Output

1