Print 'K'th least significant bit of a number Last Updated : 15 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice A number N is given. We need to print its 'K'th Least Significant Bit.Examples : Input : num = 10, k = 4 Output : 1 Explanation : Binary Representation of 10 is 1010. 4th LSB is 1. Input : num = 16, k = 3 Output : 0 Explanation : Binary Representation of 16 is 10000. 3rd LSB is 0.Recommended PracticeKth LSBTry It! We can easily solve this problem by following steps : Shift the number '1' (K-1) times left.This will yield a number with all unset bits but the 'K'th bit. Now, we'll perform logical AND of the shifted number with given number.All bits except the 'K'th bit will yield 0, and 'K'th bit will depend on the number. This is because, 1 AND 1 is 1. 0 AND 1 is 0. C++ // CPP code to print 'K'th LSB #include <bits/stdc++.h> using namespace std; //Function returns 1 if set, 0 if not bool LSB(int num, int K) { return (num & (1 << (K-1))); } //Driver code int main() { int num = 10, K = 4; //Function call cout << LSB(num, K); return 0; } java // java code to print 'K'th LSB import java .io.*; class GFG { // Function returns 1 if set, 0 if not static boolean LSB(int num, int K) { boolean x = (num & (1 << (K-1))) != 0; return (x); } // Driver code public static void main(String[] args) { int num = 10, K = 4; //Function call if(LSB(num, K)) System.out.println("1") ; else System.out.println("0"); } } // This code is contributed by Anuj_67 Python3 # Python code to print 'K'th LSB # Function returns 1 if set, 0 if not def LSB(num, K): return bool(num & (1 << (K - 1) )) # Driver code num, k = 10, 4 res = LSB(num, k) if res : print(1) else: print(0) #This code is contributed by Sachin Bisht C# // C# code to print 'K'th LSB using System; class GFG { // Function returns 1 if set, 0 if not static bool LSB(int num, int K) { bool x = (num & (1 << (K-1))) != 0; return (x); } // Driver code static void Main() { int num = 10, K = 4; //Function call if(LSB(num, K)) Console.Write("1") ; else Console.Write("0"); } } // This code is contributed by Anuj_67 PHP <?php // PHP code to print 'K'th LSB // Function returns 1 if set, 0 if not function LSB($num, $K) { return ($num & (1 << ($K - 1))); } // Driver code $num = 10; $K = 4; $r = LSB($num, $K); if($r) echo '1'; else echo '0'; // This code is contributed by Ajit ?> JavaScript <script> // Javascript code to print 'K'th LSB // Function returns 1 if set, 0 if not function LSB(num, K) { let x = (num & (1 << (K-1))) != 0; return (x); } let num = 10, K = 4; //Function call if(LSB(num, K)) document.write("1") ; else document.write("0"); </script> Output : 1 Time Complexity: O(1) Auxiliary Space: O(1) Print ‘K’th least significant bit of a number Comment R Rohit Thapliyal Improve R Rohit Thapliyal Improve Article Tags : Misc Bit Magic DSA Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like