- Related Questions & Answers
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Count numbers having 0 as a digit in C++
- Count numbers in a range having GCD of powers of prime factors equal to 1 in C++
- Program to count total number of set bits of all numbers in range 0 to n in Python
- Find position of the only set bit in C++
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Generating range of numbers 1…n in SAP HANA
- JavaScript - Accept only numbers between 0 to 255 range?
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- Count n digit numbers not having a particular digit in C++
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Find count of Almost Prime numbers from 1 to N in C++
- Find all combinations of k-bit numbers with n bits set where 1 <= n <= k in sorted order in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given a number and the task is to calculate the count of numbers from the range 0 till the given number let’s say, num having exactly one set bit

Set bits in a binary number is represented by 1. Whenever we calculate the binary number of an integer value then it is formed as the combination of 0’s and 1’s. So, the digit 1 is known as set bit in the terms of the computer.

**Input** − int num = 15

**Output** − Count of numbers having only 1 set bit in the range [0, 15] are − 4

**Explanation** − The given number is 15 therefore the range is 0-15. Now calculate the 4 digit

binary number for −

0 -> 0000 = 0 set bit, 1 -> 0001 = 1 set bit, 2 -> 0010 = 1 set bit, 3 -> 0011 = 2 set bit, 4 -> 0100 = 1 set bit, 5 -> 0101 = 2 set bit, 6 -> 0110 = 2 set bit, 7 -> 0111 = 3 set bit, 8 -> 1000 = 1 set bit, 1 -> 1001 = 2 set bit, 10 -> 1010 = 2 set bit, 11 -> 1011 = 3 set bit, 12 -> 1100 = 2 set bit, 13 -> 1101 = 3 set bit, 14 -> 1110 = 3 set bit, 15 -> 1111 = 4 set bits. Now, we will choose the numbers with exactly one set bits and those are 1, 2, 4 and 8.

**Input** − int num = 4

**Output** − Count of numbers having only 1 set bit in the range [0, 15] are − 3

**Explanation** − The given number is 4 therefore the range is 0-4. Now calculate the 4 digit binary

number for −

0 -> 0000 = 0 set bit, 1 -> 0001 = 1 set bit, 2 -> 0010 = 1 set bit, 3 -> 0011 = 2 set bit, 4 -> 0100 = 1 set bit. Now, we will choose the numbers with exactly one set bits and those are 1, 2 and 4.

There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the **naive approach**.

Input the number and pass it to the function for further processing.

Take a temporary variable count to store the count of numbers in the range with set bit as exactly 1

Start loop FOR from i to 1 till the number

Inside the loop, set a temp variable with ‘ __builtin_popcount(i)’, the function that returns the number of set bits.

Check IF temp = 1 then increment the count

Return count

Print result

Input the number and pass it to the function for further processing.

Take a temporary variable count to store the count of numbers in the range with set bit as exactly 1

Start loop While from temp till temp <= number

Inside the loop, increment the count by 1 and set temp as temp * 2

Return count

Print result

#include <iostream> using namespace std; //function to Count of numbers having only 1 set bit in the range [0, n] int set_bits(int number){ int count = 0; for (int i = 1; i <= number; i++){ int temp = __builtin_popcount(i); if (temp == 1){ count++; } } return count; } int main(){ int number = 15; cout<<"Count of numbers having only 1 set bit in the range [0, "<<number<<"] are: "<<set_bits(number); return 0; }

If we run the above code it will generate the following output −

Count of numbers having only 1 set bit in the range [0, 15] are: 4

#include <iostream> using namespace std; //function to Count of numbers having only 1 set bit in the range [0, n] int set_bits(int number){ int count = 0; int temp = 1; while(temp <= number){ count++; temp = temp *2; } return count; } int main(){ int number = 15; cout<<"Count of numbers having only 1 set bit in the range [0, "<<number<<"] are: "<<set_bits(number); return 0; }

If we run the above code it will generate the following output −

Count of numbers having only 1 set bit in the range [0, 15] are: 4

Advertisements