All submissions

Check if K-th Bit is Set

Code

#include <stdio.h>

int isKthBitSet(int n, int k) {
    int mask = (1 << k);

    n = n & mask;
    if (n){
        return 1;
    } else {
        return 0;
    }
}

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

Solving Approach

I created a mask around K-th bit and used bitwise AND operation with integer N to know if bit is set or not. 

 

 

Loading...

Input

8 3

Expected Output

1