×

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).


Related Topics

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

6 minutes read.

Intersection Point in Y Shaped Linked Lists in Java

Intersection Point in Y Shaped Linked Lists in Java In this article, we are going to see how to find the intersection point in a Y-shaped linked list. Method 1: We need to...

4 minutes read.

Primitive Data Structure in C

The data structure is a logical or mathematical model for organizing and structuring the main memory or elements. We can classify the data structures in two ways one is primitive, and...

10 minutes read.

Partitioning a linked list around a given value

Partitioning a linked list around a given value In this problem, we are given a linked list and a value k. We need to partition the given linked list so that...

3 minutes read.

All About Minimum Cost Spanning Trees in Data Structure

Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system that stores, manages, and optimizes...

7 minutes read.

LCA of binary tree

Implementation //Writing a program to find the lowest common factor in a given binary search tree. #include <iostream> #include <vector> using namespace std; // the very first step is to create a binary tree. struct __nod { int...

8 minutes read.

Bucket Sort

Bucket Sort: In the sorting algorithm, we create buckets and put elements into them. We can apply some sorting algorithm (insertion sort) to sort the elements in each bucket. Finally,...

4 minutes read.

Segregate Even and Odd nodes in a Linked List

Segregate even and odd nodes in a Linked List In this problem, we have given a linked list with integer numbers. We need to modify the given linked list in such...

4 minutes read.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

What is a Sparse Matrix in Data Structure?

Definition A matrix in which a few non-zero elements are present is called a Sparse matrix. In a Sparse matrix, almost all the matrices are filled with zero (0). A matrix...

5 minutes read.

Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and...

5 minutes read.

Quick Sort

Quicksort is a sorting algorithm that uses a divide-and-conquer strategy. A pivot element is used to divide an array into subarrays (element selected from the array).  The pivot element should be...

4 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

A Full Binary Tree with n Nodes

Implementation // Writing the implementation of the above approach in C++ #include <bits/stdc++.h> using namespace std; // We are creating a class that will create a node and its left and right children.  struct __nod...

12 minutes read.

Difference between complete and full binary tree

As we all know that the  binary tree is a tree it contains one or two children at each other node. It contains two children's nodes in the Binary tree. The...

6 minutes read.

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position...

4 minutes read.

Burning binary tree

Burn the Binary tree starting from the target node You have given a binary tree and a target node value. Now you have to burn the tree from target node. You...

4 minutes read.