POTD Solutions | 29 Oct’ 23 | Check whether K-th bit is set or not Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like 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 OctCheck 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 OctCheck whether K-th bit is set or notRead Full Solution! Create Quiz Comment M mridul_gfg Follow 0 Improve M mridul_gfg Follow 0 Improve Article Tags : DSA Algorithms-Bit Algorithms GFG-POTD-Solutions 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 15 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 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like