3. Check if K-th Bit is Set

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int isKthBitSet(int n, int k) {
    // Write your code here
    int ret_val =0;
    int val = (1<<k);
    
    // If AND operation of n and 
    // value is non-zero, it means
    // k'th bit is set 
    if ((n&val) != 0) {
       ret_val =1;
    } 
    return ret_val;

}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote