POTD Solutions | 29 Oct’ 23 | Check whether K-th bit is set or not Last Updated : 22 Nov, 2023 Improve Improve Like Article Like Save Share Report View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Bit Manipulation but will also help you build up problem-solving skills. POTD 29 Oct Solution POTD 29th Oct Check whether K-th bit is set or not Solve! Watch! POTD 29 October: Check whether K-th bit is set or not: Given a number N and a bit number K, check if the Kth index bit of N is set or not. A bit is called set if it is 1. The position of set bit ‘1‘ should be indexed starting with 0 from the LSB side in the binary representation of the number. Example: Input: N = 4, K = 0Output: NoExplanation: The binary representation of 4 is 100, in which the 0th index bit from LSB is not set. So, return false. Input: N = 4, K = 2Output: YesExplanation: Binary representation of 4 is 100, in which 2nd index bit from LSB is set. So, return true. Check whether K-th bit is set or not using Bitwise Operator: The idea is to use Left Shift (<<) Operator. Left shift number 1 by K to create a number that has only set bit as K-th bit and store that number in temp variable. If bitwise AND of N and temp is non-zero, then K-th bit is SET else K-th bit is NOT SET. Follow the steps to solve the above problem: Create a variable temp. Left shift number 1 by K and store it in temp. Now temp will have only the K-th bit set. Perform Bitwise AND of temp and N: If Bitwise AND comes out to be non-zero then the K-th bit of N is set and hence return true. If Bitwise AND comes out to be zero then the K-th bit of N is not set and hence return false. Below is the implementation of above approach: C++ class Solution { public: // Function to check if Kth bit is set or not. bool checkKthBit(int n, int k) { // Create a variable temp with the Kth bit set to 1. int temp = (1 << k); // Perform a bitwise AND operation between 'n' and // the temp. If the result is not 0, it means the // Kth bit is set, so return true. if ((n & temp) != 0) return true; else // If the Kth bit is not set, return false. return false; } }; Java class CheckBit { // Function to check if Kth bit is set or not. static boolean checkKthBit(int n, int k) { // Create a variable temp with the Kth bit set to 1. int temp = (1 << k); // Perform a bitwise AND operation between 'n' and // the temp. If the result is not 0, it means the // Kth bit is set, so return true. if ((n & temp) != 0) return true; else // If the Kth bit is not set, return false. return false; } } Python3 class Solution: #Function to check if Kth bit is set or not. def checkKthBit(self, n,k): # Create a variable temp with the Kth bit set to 1. temp = (1 << k) # Perform a bitwise AND operation between 'n' and # the temp. If the result is not 0, it means the # Kth bit is set, so return True. if (n & temp) != 0: return True else: # If the Kth bit is not set, return False. return False Time Complexity: O(1), Left shift, right shift operator takes O(1) time.Auxiliary Space: O(1), We are not using any extra space. POTD 29th Oct Check whether K-th bit is set or not Read Full Solution! Like Article Suggest improvement Next Check whether K-th bit is set or not Share your thoughts in the comments Add Your Comment Please Login to comment... Similar Reads POTD Solutions | 31 Oct’ 23 | Move all zeroes to end of array POTD Solutions | 15 Oct' 23 | Normal BST to Balanced BST POTD Solutions | 16 Oct’ 23 | Making A Large Island POTD Solutions | 17 Oct’ 23 | Transitive Closure of a Graph POTD Solutions | 18 Oct’ 23 | Eventual Safe States POTD Solutions | 19 Oct’ 23 | Level of Nodes POTD Solutions | 20 Oct’ 23 | Form a number divisible by 3 using array digits POTD Solutions | 21 Oct’ 23 | Sum of all divisors from 1 to n POTD Solutions | 22 Oct’ 23 | Number of paths POTD Solutions | 23 Oct’ 23 | Maximum Sum Increasing Subsequence Like M mridul_gfg Follow Article Tags : Algorithms-Bit Algorithms GFG-POTD-Solutions Competitive Programming DSA