Check whether K-th bit is set or not c++
Check whether K-th bit is set or not in C++
#include <iostream>
using namespace std;
bool isKthBitSet(int n, int k) {
if (n & (1 << (k - 1)))
return true;
else
return false;
}
int main() {
int n = 20, k = 3;
if (isKthBitSet(n, k))
cout << "K-th bit is set";
else
cout << "K-th bit is not set";
return 0;
}