- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# 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)

#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)

#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

- Related Questions & Answers
- Count numbers in a range having GCD of powers of prime factors equal to 1 in C++
- Find position of the only set bit in C++
- Print numbers having first and last bits as the only set bits
- C++ Program to count ordinary numbers in range 1 to n
- Program to find count of numbers having odd number of divisors in given range 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++
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Generating range of numbers 1…n in SAP HANA
- Count subsets having distinct even numbers in C++
- Count numbers from range whose prime factors are only 2 and 3 in C++
- Find the number of divisors of all numbers in the range [1, n] in C++
- Python Count set bits in a range?
- Count Unary Numbers in a Range in C++
- Count number of triplets in an array having sum in the range [a,b] in C++