Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced

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



ADVERTISEMENT
ADVERTISEMENT