×

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

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

6 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique. Hash table We...

4 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Hash Table vs STL Map

Hash table and STL map are extremely valuable information structures in software engineering. Here we will consider the examination between their properties to be well as execution.  To start with, we will...

7 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Flatten Binary Tree to a linked list

Implementation In this section, we will see the implementation of the binary Tree and its conversion into linked lists. let us proceed: - // Writing a C++ program that will convert a...

4 minutes read.

Insertion sort

Insertion sort is a simple sorting technique. It is best suited for small data sets, but it does not suitable for large data sets. In this technique, we pick an...

4 minutes read.

Binary Tree vs Binary Search Tree: Data Structure

Difference Between Binary Tree and Binary Search Tree What is Binary Tree? A tree which each node can have utmost two children called binary tree. These children are referred as the ‘left...

3 minutes read.

What is a Threaded Binary Tree?

When we consider those binary trees that are interlinked with each other, we do come across the fact that the fields present in there do consist of NULL values that...

3 minutes read.

Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method. What is recursion? Defining anything in terms...

7 minutes read.

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.

Vertical Order Traversal of Binary Tree

Implementation #include <iostream> #include <vector> #include <map> using namespace std; // representing the primary model of a binary tree node. struct _nod { int ky; _nod *Lft, *Rt; }; // establishing a new function representing the new binary tree node. struct _nod*...

5 minutes read.

CSS Text-indent

Text-indent The Text-indent property of CSS is used to set any first line’s indentation inside a text’s block. It describes the horizontal space amount that puts establish before the text line. It...

3 minutes read.

Given a Binary Tree, find its Minimum Depth

Implementation // Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree.  #include<bits/stdc++.h> using namespace std; // Creating a new binary tree node struct __nod { int record; struct __nod*...

5 minutes read.

About Data Structures

What exactly are data structures? A data structure is a type of storage that is used to organise and store data. It is a method of organising data on a computer...

5 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Strictly binary tree in Data Structures?

What is a strictly Binary Tree in Data Structures? There are various kinds of binary trees that we know exist in data structures, and they all have their purposes. In this...

4 minutes read.

2-3 Trees and Basic Operations on them

2-3 Trees, like any other AVL trees or B-trees, are just a type of Height Balanced Tree. 2-3 Trees are the B-trees of order 3. Like every other B-tree, the...

4 minutes read.

Reverse a Linked List in groups of given size

Reverse a Linked List in groups of given size This article will explain how to reverse a linked list in groups of given size. Here we have given a linked list...

2 minutes read.