Given a number n and a bit position k, check if the kth bit of n is set or not. A bit is called set if it is 1 at that position.
Note: Indexing starts with 0 from LSB (least significant bit) side in the binary representation of the number.
Examples:
Input: n = 7, k = 2
Output: true
Explanation: 7 is represented as 111 in binary and bit at position 2 is set.Input: n = 5, k = 1
Output: false
Explanation: 5 is represented as 101 in binary and bit at position 1 is not set.
Table of Content
[Approach 1] Using Left Shift Operator - O(k) Time and O(1) Space
The idea is to leverage bitwise operations to check if a specific bit is set. Create a number that has only the k-th bit set (by left-shifting 1 by k positions). Then we perform a bitwise AND operation between this number and the given number n. If the result is non-zero, it means the k-th bit in n is set to 1, otherwise it's 0.
#include <iostream>
using namespace std;
bool checkKthBit(int n, int k) {
// Value whose only kth bit
// is set.
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) {
return true;
}
return false;
}
int main() {
int n = 7, k = 2;
if (checkKthBit(n, k)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
class GfG {
static boolean checkKthBit(int n, int k) {
// Value whose only kth bit
// is set.
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) {
return true;
}
return false;
}
public static void main(String[] args) {
int n = 7, k = 2;
if (checkKthBit(n, k)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
def checkKthBit(n, k):
# Value whose only kth bit
# is set.
val = (1 << k)
# If AND operation of n and
# value is non-zero, it means
# k'th bit is set
if (n & val) != 0:
return True
return False
if __name__ == "__main__":
n, k = 7, 2
if checkKthBit(n, k):
print("true")
else:
print("false")
using System;
class GfG {
static bool checkKthBit(int n, int k) {
// Value whose only kth bit
// is set.
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) {
return true;
}
return false;
}
static void Main() {
int n = 7, k = 2;
if (checkKthBit(n, k)) {
Console.WriteLine("true");
} else {
Console.WriteLine("false");
}
}
}
function checkKthBit(n, k) {
// Value whose only kth bit
// is set.
let val = (1 << k);
// If AND operation of n and
// value is non-zero, it means
// k'th bit is set
if ((n & val) !== 0) {
return true;
}
return false;
}
let n = 7, k = 2;
if (checkKthBit(n, k)) {
console.log("true");
} else {
console.log("false");
}
Output
true
[Approach 2] Using Right Shift Operator - O(k) Time and O(1) Space
The idea is to shift the bits of the given number to the right by k positions, which brings the k-th bit to the rightmost position (0th position). Then we perform a bitwise AND operation with 1. If this result is non-zero (meaning the bit is 1), then the k-th bit in the original number was set, otherwise it wasn't.
#include <iostream>
using namespace std;
bool checkKthBit(int n, int k) {
// Right shift n by k
n = n >> k;
// If 0th bit is set
if ((n & 1) != 0) {
return true;
}
return false;
}
int main() {
int n = 7, k = 2;
if (checkKthBit(n, k)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
class GfG {
static boolean checkKthBit(int n, int k) {
// Right shift n by k
n = n >> k;
// If 0th bit is set
if ((n & 1) != 0) {
return true;
}
return false;
}
public static void main(String[] args) {
int n = 7, k = 2;
if (checkKthBit(n, k)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
def checkKthBit(n, k):
# Right shift n by k
n = n >> k
# If 0th bit is set
if (n & 1) != 0:
return True
return False
if __name__ == "__main__":
n, k = 7, 2
if checkKthBit(n, k):
print("true")
else:
print("false")
using System;
class GfG {
static bool checkKthBit(int n, int k) {
// Right shift n by k
n = n >> k;
// If 0th bit is set
if ((n & 1) != 0) {
return true;
}
return false;
}
static void Main() {
int n = 7, k = 2;
if (checkKthBit(n, k)) {
Console.WriteLine("true");
} else {
Console.WriteLine("false");
}
}
}
function checkKthBit(n, k) {
// Right shift n by k
n = n >> k;
// If 0th bit is set
if ((n & 1) !== 0) {
return true;
}
return false;
}
let n = 7, k = 2;
if (checkKthBit(n, k)) {
console.log("true");
} else {
console.log("false");
}
Output
true