Check if K-th Bit is Set

Code

#include <stdio.h>

int isKthBitSet(int n, int k) {
    if (n & (1 << k) )  // shift 1 by (k-1) positions and AND with n
        return 1;             // bit is set
    else
        return 0;             // bit is not set
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

8 3

Expected Output

1