# 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

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 only 1 set bit.

Input:n = 3

Output:2

**Approach:** If **k** bits are required to represent **n** then there are **k** numbers possible as **1 can be positioned at k different positions** each time.

Below is the implementation of the above approach

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the required count ` `int` `count(` `int` `n) ` `{ ` ` ` ` ` `// To store the count of numbers ` ` ` `int` `cnt = 0; ` ` ` `int` `p = 1; ` ` ` `while` `(p <= n) { ` ` ` `cnt++; ` ` ` ` ` `// Every power of 2 contains ` ` ` `// only 1 set bit ` ` ` `p *= 2; ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 7; ` ` ` `cout << count(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the required count ` ` ` `static` `int` `count(` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the count of numbers ` ` ` `int` `cnt = ` `0` `; ` ` ` `int` `p = ` `1` `; ` ` ` `while` `(p <= n) { ` ` ` `cnt++; ` ` ` ` ` `// Every power of 2 contains ` ` ` `// only 1 set bit ` ` ` `p *= ` `2` `; ` ` ` `} ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `7` `; ` ` ` `System.out.print(count(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to return the required count ` ` ` `static` `int` `count(` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the count of numbers ` ` ` `int` `cnt = 0; ` ` ` `int` `p = 1; ` ` ` `while` `(p <= n) { ` ` ` `cnt++; ` ` ` ` ` `// Every power of 2 contains ` ` ` `// only 1 set bit ` ` ` `p *= 2; ` ` ` `} ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 7; ` ` ` `Console.Write(count(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the required count ` `def` `count(n): ` ` ` ` ` `# To store the count of numbers ` ` ` `cnt ` `=` `0` ` ` `p ` `=` `1` ` ` `while` `(p <` `=` `n): ` ` ` `cnt ` `=` `cnt ` `+` `1` ` ` ` ` `# Every power of 2 contains ` ` ` `# only 1 set bit ` ` ` `p ` `*` `=` `2` ` ` `return` `cnt ` ` ` `# Driver code ` `n ` `=` `7` `print` `(count(n)); ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the required count ` `function` `count_t(` `$n` `) ` `{ ` ` ` ` ` `// To store the count of numbers ` ` ` `$cnt` `= 0; ` ` ` `$p` `= 1; ` ` ` `while` `(` `$p` `<= ` `$n` `) ` ` ` `{ ` ` ` `$cnt` `++; ` ` ` ` ` `// Every power of 2 contains ` ` ` `// only 1 set bit ` ` ` `$p` `*= 2; ` ` ` `} ` ` ` `return` `$cnt` `; ` `} ` ` ` `// Driver code ` `$n` `= 7; ` `echo` `count_t(` `$n` `); ` ` ` `// This Code is contributed by ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(log n)

## Recommended Posts:

- Count Odd and Even numbers in a range from L to R
- Count factorial numbers in a given range
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count of numbers from range [L, R] whose sum of digits is Y
- Count the numbers divisible by 'M' in a given range
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count all the numbers in a range with smallest factor as K
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count numbers with unit digit k in given range
- Count of common multiples of two numbers in a range
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count numbers from range whose prime factors are only 2 and 3
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count of distinct remainders when N is divided by all the numbers from the range [1, N]

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.