Check if K-th Bit is Set

Code

/* This code is to check kth bit is set
   submitted by Manish Gowda T
   accomplished by leftshift and AND function*/


#include <stdio.h>

int isKthBitSet(int n, int k) {
return (n & (1 << k)) != 0;
}

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

Solving Approach

First we left shift by kbits so only that bit it set 
then we perform AND operation to check if the result is not equal to 0

 

 

Upvote
Downvote
Loading...

Input

8 3

Expected Output

1