All submissions

Check if K-th Bit is Set

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

We cannot just use bitwise AND to a bit-shifted 1, since it could output a result that is greater than 1 (which may result to undefined behavior) 

So instead we bitshift the given integer first and then perform an AND to the last bit so that it returns only either 1 or 0.

 

 

Loading...

Input

8 3

Expected Output

1