Count numbers up to N having Kth bit set
Last Updated :
31 Mar, 2023
Given two integers N and K, the task is to find the count of numbers up to N having K-th (0-based indexing) bit set.
Examples:
Input: N = 14, K = 2
Output: 7
Explanation:
The numbers less than equal to 14, having 2nd bit set are 4, 5, 6, 7, 12, 13, and 14.
Input: N = 6, K = 1
Output: 3
Explanation:
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.
Below is the implementation of the above approach:
C++
// 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 Code
int main()
{
long long int N = 14;
int K = 2;
// Function Call
cout << getcount(N + 1, K) << endl;
return 0;
}
Java
// Java program for above approach
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
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
# 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 Code
if __name__ == '__main__':
N = 14
K = 2
# Function Call
print (getcount(N + 1, K))
# This code is contributed by mohit kumar 29
C#
// C# program for above approach
using 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
JavaScript
<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>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Count numbers up to N whose rightmost set bit is K Given two positive integers N and K, the task is to count the numbers from the range [1, N] whose Kth bit from the right, i.e. LSB, is the rightmost set bit. Examples: Input: N = 15, K = 2Output: 4Explanation:(2)10 = (010)2, (6)10 = (110)2, (10)10 = (1010)2, (14)10 = (1110)2 have the 2nd bit from th
5 min read
Count of numbers having only 1 set bit in the range [0, n] Given an integer n, the task is to count the numbers having only 1 set bit in the range [0, n].Examples: Input: n = 7 Output: 3 Explanation: 000, 001, 010, 011, 100, 101, 110 and 111 are the binary representation of all the numbers upto 7. And there are only 3 numbers ( 001, 010 and 100 ) having onl
3 min read
Count of numbers having only one unset bit in a range [L,R] Given two integers L and R, the task is to count the numbers having only one unset bit in the range [L, R]. Examples: Input: L = 4, R = 9Output: 2Explanation:The binary representation of all numbers in the range [4, 9] are 4 = (100)2 5 = (101)2 6 = (110)2 7 = (111)2 8 = (1000)2 9 = (1001)2 Out of al
11 min read
Count total unset bits in all the numbers from 1 to N Given a positive integer N, the task is to count the total number of unset bits in the binary representation of all the numbers from 1 to N. Note that leading zeroes will not be counted as unset bits.Examples: Input: N = 5 Output: 4 IntegerBinary RepresentationCount of unset bits11021013110410025101
4 min read
Count total bits in a number Given a positive number n, count total bit in it.Examples: Input : 13 Output : 4 Binary representation of 13 is 1101 Input : 183 Output : 8 Input : 4096 Output : 13 Method 1 (Using Log) The log2(n) logarithm in base 2 of n, which is the exponent to which 2 is raised to get n only integer and we add
7 min read