Count numbers up to N having Kth bit set

23 Apr, 2021
Given two integers N and K, the task is to find the count of numbers up to N having K-th bit set.
Examples:
Input: N = 14, K = 2Output: 7Explanation: The numbers less than equal to 14, having 2nd bit set are 4, 5, 6, 7, 12, 13, and 14.
Input: N = 6, K = 1Output: 3Explanation: The numbers less than equal to 6 having 1st bit set are 1, 3, 5.
Naive Approach: The simplest approach is to traverse from 1 to N, and check for each number whether its K-th bit is set or not.
Time Complexity: O(N)Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by dividing the task into two parts:
First, right shift N, K+1 times followed by left shifting the result K times, which gives the count of numbers satisfying the given condition till the nearest power of 2 less than N.Now, check if the Kth bit is set in N or not.If the Kth bit is set in N, then add the count of numbers from the nearest power of 2 less than N to the answer.
First, right shift N, K+1 times followed by left shifting the result K times, which gives the count of numbers satisfying the given condition till the nearest power of 2 less than N.
Now, check if the Kth bit is set in N or not.
If the Kth bit is set in N, then add the count of numbers from the nearest power of 2 less than N to the answer.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for above approach#include <bits/stdc++.h>using namespace std; // Function to return the count// of number of 1's at ith bit// in a range [1, n - 1]long long getcount(long long n, int k){    // Store count till nearest    // power of 2 less than N    long long res = (n >> (k + 1)) << k;     // If K-th bit is set in N    if ((n >> k) & 1)         // Add to result the nearest        // power of 2 less than N        res += n & ((1ll << k) - 1);     // Return result    return res;} // Driver Codeint main(){     long long int N = 14;    int K = 2;     // Function Call    cout << getcount(N + 1, K) << endl;     return 0;}
// Java program for above approachclass GFG{   // Function to return the count  // of number of 1's at ith bit  // in a range [1, n - 1]  static long getcount(long n, int k)  {     // Store count till nearest    // power of 2 less than N    long res = (n >> (k + 1)) << k;     // If K-th bit is set in N    if (((n >> k) & 1) != 0)       // Add to result the nearest      // power of 2 less than N      res += n & ((1 << k) - 1);     // Return result    return res;  }   // Driver code  public static void main(String[] args)  {    long N = 14;    int K = 2;     // Function Call    System.out.println(getcount(N + 1, K));  }} // This code is contributed by divyesh072019
# Python3 program for above approach # Function to return the count# of number of 1's at ith bit# in a range [1, n - 1]def getcount(n, k):         # Store count till nearest    # power of 2 less than N    res = (n >> (k + 1)) << k     # If K-th bit is set in N    if ((n >> k) & 1):                 # Add to result the nearest        # power of 2 less than N        res += n & ((1 << k) - 1)     # Return result    return res # Driver Codeif __name__ == '__main__':     N = 14    K = 2     # Function Call    print (getcount(N + 1, K)) # This code is contributed by mohit kumar 29
// C# program for above approachusing System; class GFG{ // Function to return the count// of number of 1's at ith bit// in a range [1, n - 1]static long getcount(long n, int k){         // Store count till nearest    // power of 2 less than N    long res = (n >> (k + 1)) << k;         // If K-th bit is set in N    if (((n >> k) & 1) != 0)             // Add to result the nearest        // power of 2 less than N        res += n & ((1 << k) - 1);         // Return result    return res;} // Driver Code static void Main(){    long N = 14;    int K = 2;         // Function Call    Console.WriteLine(getcount(N + 1, K));}} // This code is contributed by divyeshrabadiya07
<script>     // Javascript program for above approach         // Function to return the count    // of number of 1's at ith bit    // in a range [1, n - 1]    function getcount(n, k)    {         // Store count till nearest        // power of 2 less than N        let res = (n >> (k + 1)) << k;         // If K-th bit is set in N        if (((n >> k) & 1) != 0)             // Add to result the nearest            // power of 2 less than N            res += n & ((1 << k) - 1);         // Return result        return res;    }         let N = 14;    let K = 2;          // Function Call    document.write(getcount(N + 1, K));   </script>
7
 
Time Complexity: O(1)Auxiliary Space: O(1)
mohit kumar 29
divyesh072019
divyeshrabadiya07
rameshtravel07
maths-power
setBitCount
Bit Magic
Mathematical
Searching
Searching
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.