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 & (1 << k)) != 0 ? 1 : 0;
}

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

Solving Approach

Note that the & operator has lower precedence than comparison operation; so, if the ternary conditional were written as:

    `(n & (1 << k) != 0)`

the expression (1 << k) != 0 would be evaluated before the bitwise AND operation.

 

 

 

Loading...

Input

8 3

Expected Output

1