Print 'K'th least significant bit of a number - GeeksforGeeks

31 Jan, 2022
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.
 
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.
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++
java
Python3
C#
PHP
Javascript
// CPP code to print 'K'th LSB#include <bits/stdc++.h>using namespace std; //Function returns 1 if set, 0 if notbool LSB(int num, int K){    return (num & (1 << (K-1)));} //Driver codeint main(){    int num = 10, K = 4;         //Function call    cout << LSB(num, K);         return 0;}
// java code to print 'K'th LSBimport 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
# Python code to print 'K'th LSB # Function returns 1 if set, 0 if notdef LSB(num, K):    return bool(num & (1 << (K - 1) )) # Driver codenum, k = 10, 4 res = LSB(num, k)if res :    print(1)else:    print(0) #This code is contributed by Sachin Bisht
// C# code to print 'K'th LSBusing 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 code to print 'K'th LSB // Function returns 1 if set, 0 if notfunction 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?>
<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
 
YouTubeGeeksforGeeks507K subscribersPrint ‘K’th least significant bit of a number | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:18•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=tB7Qp4copus" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
vt_m
rameshtravel07
amartyaghoshgfg
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Find the size of Largest Subset with positive Bitwise AND
Check whether bitwise AND of a number with any subset of an array is zero or not
Write an Efficient Method to Check if a Number is Multiple of 3
Highest power of 2 less than or equal to given number
Swap two nibbles in a byte
Check for Integer Overflow
Swap bits in a given number
Reverse actual bits of the given number
Bit Tricks for Competitive Programming