3. Check if K-th Bit is Set

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

/*
 * Đề bài là kiểm tra bit thứ k là 0 hay 1:
 * Ví dụ bit thứ 4: 
   (1001 0011 >> 4) & 1 = 0000 1001 & 1 = 0000 0001 = 1 
  => (n >> k) & 1
*/

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

 

 

 

Was this helpful?
Upvote
Downvote