Print 'K'th least significant bit of a number Last Updated : 15 Jul, 2022 Summarize Comments Improve Suggest changes Share 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) Comment More infoAdvertise with us Next Article Print 'K'th least significant bit of a number R Rohit Thapliyal Improve Article Tags : Misc Bit Magic DSA Practice Tags : Bit MagicMisc Similar Reads Unset least significant K bits of a given number Given an integer N, the task is to print the number obtained by unsetting the least significant K bits from N. Examples: Input: N = 200, K=5Output: 192Explanation: (200)10 = (11001000)2 Unsetting least significant K(= 5) bits from the above binary representation, the new number obtained is (11000000 4 min read Find most significant set bit of a number Given a number, find the greatest number less than the given a number which is the power of two or find the most significant bit number . Examples: Input: 10Output: 8Explanation:Greatest number which is a Power of 2 less than 10 is 8Binary representation of 10 is 1010The most significant bit corresp 15 min read Find most significant bit of a number X in base Y Given two positive integers X and Y, the task is to find the MSB of X, in the given base Y.Examples: Input: X = 55, Y = 3 Output: 2 Explanation: 55 is 2001 in base 3 with first digit as 2.Input: X = 123, Y = 10 Output: 1 Explanation: 123 is 123 in base 10 with first digit 1. Approach: Let the task t 4 min read Invert the Kth most significant bit of N Given two non-negative integers N and K, the task is to invert the Kth most significant bit of N and print the number obtained after inverting the bit.Examples: Input: N = 10, K = 1 Output: 2 The binary representation of 10 is 1010. After inverting the first bit it becomes 0010 whose decimal equival 7 min read Program to clear K-th bit of a number N Given a number N, the task is to clear the K-th bit of this number N. If K-th bit is 1, then clear it to 0 and if it is 0 then leave it unchanged.Examples: Input: N = 5, K = 1 Output: 4 5 is represented as 101 in binary and has its first bit 1, so clearing it will result in 100 i.e. 4. Input: N = 5, 3 min read Like