# Count of numbers having only 1 set bit in the range [0, n] in C++

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.

## Approach used in the below program is as follows

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

## Efficient 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 While from temp till temp <= number

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

• Return count

• Print result

## Example (naive approach)

Live Demo

#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;
}

## Output

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

## Example (Efficient approach)

Live Demo

#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;
}

## Output

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

Updated on: 02-Nov-2020

112 Views