# Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from the number’s binary form.

Input- Number = 10, K = 2

Output- 1

Explanation- If we convert the number to binary then it will be 1010. So, we find the second least significant bit is 1.

Input- Number = 2, K = 2

Output- 1

Explanation- If we convert the number to binary then it will be 11. So, we find the second least significant bit is 1.

Input- Number = 8, K = 1

Output- 0

Explanation- If we convert the number to binary then it will be 1000. So, we find the first least significant bit is 0.

Input- Number = 0, K = 1

Output- 0

Explanation- If we convert the number to binary then it will be 0. So, we find the first least significant bit is 0.

Note:

What is the least significant bit?

Every digits of a number can be called as bit. In binary representation of a number, the last digit is least significant because it adds lowest value to the number.

### Algorithm:

Step 1: Start

Step 2: Two values are taken from the user

Step 3: A function is called. This function takes two input values.

Step 4: We calculate the value by using bit operator.

Step 5: The answer value will be returned.

Step 6: The returned value will be printed.

Step 7: Stop.

Explanation of Algorithm

First we shift the number ‘1’ (K-1) times left using bit operation. This process will give a number with all unset bits but the ‘K’th bit. After that logical AND of the shifted number with given number will be done. All bits except the ‘K’th bit will yield 0, and ‘K’th bit will depend on the number. It happens because, 1 AND 1 is 1. 0 AND 1 is 0.

Code:

Program in C++

``````// Finding kth least significant bit using C++ program
#include <bits/stdc++.h>
using namespace std;
//The function to check whether set or not
bool LEASTBITFIND(int num, int K)
{
return (num & (1 << (K-1)));
}

//Main function to implement the code
int main()
{
int num = 10, K = 4;

//Calling the other function
cout << LEASTBITFIND(num, K);

return 0;
}
``````

Program in Java

``````// Finding kth least significant bit using java program
import java .io.*;
class bno {

// The function to check whether set or not
static boolean LEASTBITFIND(int num, int K)
{
boolean x = (num & (1 << (K-1))) != 0;
return (x);
}

// Main function to implement the code
public static void main(String[] args)
{
int num = 10, K = 4;

//Calling the other function
if(LEASTBITFIND(num, K))
System.out.println("1") ;

else
System.out.println("0");
}
}
``````

Program in Python

``````# Finding kth least significant bit using python program
# The function to check whether set or not
def LEASTBITFIND(num, K):
return bool(num & (1 << (K - 1) ))

# Main function to implement the code
num, k = 10, 4

res = LEASTBITFIND(num, k)
if res :
print(1)
else:
print(0)
``````

Program in C#

``````// Finding kth least significant bit using c# program
using System;
class bno {
// The function to check whether set or not
static bool LEASTBITFIND(int num, int K)
{
bool x = (num & (1 << (K-1))) != 0;
return (x);
}

// Main function to implement the code
static void Main()
{
int num = 10, K = 4;

//Calling the other function
if(LEASTBITFIND(num, K))
Console.Write("1") ;

else
Console.Write("0");
}
}
<?php
``````

Program in PHP

``````// Finding kth least significant bit using PHP program
// The function to check whether set or not
function LEASTBITFIND(\$num, \$K)
{
return (\$num & (1 << (\$K - 1)));
}

// Main function to implement the code
\$num = 10;
\$K = 4;

\$r = LEASTBITFIND(\$num, \$K);
if(\$r)
echo '1';
else
echo '0';
?>
<script>
``````

Program in JavaScript

``````// Finding kth least significant bit using JavaScript program

// The function to check whether set or not
function LEASTBITFIND(num, K)
{
let x = (num & (1 << (K-1))) != 0;
return (x);
}

let num = 10, K = 4;

//Calling the other function
if(LEASTBITFIND(num, K))
document.write("1") ;

else
document.write("0");

</script>
``````

Output:

`1`

### Complexity Analysis

Time complexity- Here, we don’t use any loop or recursion. So, the time complexity will be constant. Complexity will be O(1).

Space complexity- In this solution, we use constant memory. So, space complexity will be O(1).