Given two integers n and k, determine the value of the kth Least Significant Bit (LSB) in the binary representation of n. The Least Significant Bit (LSB) is the rightmost bit and is considered the 1st bit from the right.
Examples :ย
Input: n = 10, k = 4
Output: 1
Explanation: The binary representation of 10 is 1010.Counting bits from right to left:
1st LSB = 0
2nd LSB = 1
3rd LSB = 0
4th LSB = 1
Therefore, the 4th LSB is 1.
Input: n = 16, k = 3
Output: 0
Explanation: The binary representation of 16 is 10000. Counting bits from right to left:
1st LSB = 0
2nd LSB = 0
3rd LSB = 0
Therefore, the 3rd LSB is 0.
Table of Content
[Naive Approach] Convert to Binary Array - O(log n) Time and O(log n) Space
The idea is to first convert the given number into its binary representation and store all the bits in an array (or vector) from Least Significant Bit (LSB) to Most Significant Bit (MSB). Once the binary digits are stored, simply access the (k - 1)th index (0-based) if it exists. If the binary representation has fewer than k bits, then the kth LSB is 0.
Working of Approach:
- Traverse the number by repeatedly taking n % 2 and store each remainder in a vector. This stores the binary representation from LSB to MSB.
- Divide n by 2 after every iteration until it becomes 0.
- If the vector contains fewer than k bits, then the kth LSB is 0, so return 0.
- Otherwise, return the element at index k - 1, which represents the kth Least Significant Bit.
#include <iostream>
#include <vector>
using namespace std;
int kthLSB(int n, int k)
{
vector<int> bits;
// Store binary representation from LSB to MSB.
while (n > 0)
{
bits.push_back(n % 2);
n /= 2;
}
// If kth bit does not exist, it is 0.
if (k > bits.size())
return 0;
return bits[k - 1];
}
int main()
{
int n = 10, k = 4;
cout << kthLSB(n, k);
return 0;
}
import java.util.ArrayList;
class GFG {
static int kthLSB(int n, int k)
{
ArrayList<Integer> bits = new ArrayList<>();
// Store binary representation from LSB to MSB.
while (n > 0) {
bits.add(n % 2);
n /= 2;
}
// If kth bit does not exist, it is 0.
if (k > bits.size())
return 0;
return bits.get(k - 1);
}
public static void main(String[] args)
{
int n = 10, k = 4;
System.out.println(kthLSB(n, k));
}
}
def kthLSB(n, k):
bits = []
# Store binary representation from LSB to MSB.
while n > 0:
bits.append(n % 2)
n //= 2
# If kth bit does not exist, it is 0.
if k > len(bits):
return 0
return bits[k - 1]
if __name__ == "__main__":
n = 10
k = 4
print(kthLSB(n, k))
using System;
using System.Collections.Generic;
class GFG {
static int kthLSB(int n, int k)
{
List<int> bits = new List<int>();
// Store binary representation from LSB to MSB.
while (n > 0) {
bits.Add(n % 2);
n /= 2;
}
// If kth bit does not exist, it is 0.
if (k > bits.Count)
return 0;
return bits[k - 1];
}
static void Main()
{
int n = 10, k = 4;
Console.WriteLine(kthLSB(n, k));
}
}
// Store binary representation from LSB to MSB.
function kthLSB(n, k)
{
let bits = [];
// Store binary representation from LSB to MSB.
while (n > 0) {
bits.push(n % 2);
n = Math.floor(n / 2);
}
// If kth bit does not exist, it is 0.
if (k > bits.length)
return 0;
return bits[k - 1];
}
// Driver Code
let n = 10;
let k = 4;
console.log(kthLSB(n, k));
Output
1
[Expected Approach] Using Bit Manipulation - O(1) Time and O(1) Space
The idea is to create a bitmask having only the kth bit set using
(1 << (k - 1)). Then perform a bitwise AND betweennand this mask.
- If the result is non-zero, the kth LSB is 1.
- Otherwise, the kth LSB is 0.
Let us understand with an example:
Input: n = 10, k = 4
- Binary representation of 10 is 1010, and we need to find the 4th LSB.
- Create a mask by shifting 1 left by (4 - 1) positions: 1 << 3 = 1000.
- Perform bitwise AND: 1010 & 1000 = 1000.
- Since the result is non-zero, the 4th LSB is set (1).
- Therefore, the function returns 1.
#include <iostream>
#include <vector>
using namespace std;
int kthLSB(int n, int k)
{
// Bitwise AND of n and (1<<k-1)
// to check if the kth bit is 1 or 0.
if (n & (1 << (k - 1)))
return 1;
// Return 0 if the kth bit is 0.
return 0;
}
int main()
{
int n = 10, k = 4;
cout << kthLSB(n, k);
return 0;
}
class GFG {
static int kthLSB(int n, int k)
{
// Bitwise AND of n and (1<<k-1)
// to check if the kth bit is 1 or 0.
if ((n & (1 << (k - 1))) != 0)
return 1;
// Return 0 if the kth bit is 0.
return 0;
}
public static void main(String[] args)
{
int n = 10, k = 4;
System.out.println(kthLSB(n, k));
}
}
def kthLSB(n, k):
# Bitwise AND of n and (1<<k-1)
# to check if the kth bit is 1 or 0.
if n & (1 << (k - 1)):
return 1
# Return 0 if the kth bit is 0.
return 0
if __name__ == "__main__":
n = 10
k = 4
print(kthLSB(n, k))
using System;
class GFG {
static int kthLSB(int n, int k)
{
// Bitwise AND of n and (1<<k-1)
// to check if the kth bit is 1 or 0.
if ((n & (1 << (k - 1))) != 0)
return 1;
// Return 0 if the kth bit is 0.
return 0;
}
static void Main()
{
int n = 10, k = 4;
Console.WriteLine(kthLSB(n, k));
}
}
function kthLSB(n, k)
{
// Bitwise AND of n and (1<<k-1)
// to check if the kth bit is 1 or 0.
if (n & (1 << (k - 1)))
return 1;
// Return 0 if the kth bit is 0.
return 0;
}
// Driver Code
let n = 10;
let k = 4;
console.log(kthLSB(n, k));
Output
1