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

Integer Binary Representation Count of unset bits 1 1 0 2 10 1 3 11 0 4 100 2 5 101 1 0 + 1 + 0 + 2 + 1 = 4

Input:N = 14

Output:17

**Approach:**

- Iterate the loop from
**1**to**N**. - While number is greater than
**0**divide it by**2**and check the remainder. - If remainder is equal to
**0**then increase the value of**count**by**1**.

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 count of unset ` `// bits in the binary representation of ` `// all the numbers from 1 to n ` `int` `countUnsetBits(` `int` `n) ` `{ ` ` ` ` ` `// To store the count of unset bits ` ` ` `int` `cnt = 0; ` ` ` ` ` `// For every integer from the range [1, n] ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` ` ` `// A copy of the current integer ` ` ` `int` `temp = i; ` ` ` ` ` `// Count of unset bits in ` ` ` `// the current integer ` ` ` `while` `(temp) { ` ` ` ` ` `// If current bit is unset ` ` ` `if` `(temp % 2 == 0) ` ` ` `cnt++; ` ` ` ` ` `temp = temp / 2; ` ` ` `} ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `cout << countUnsetBits(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count of unset ` ` ` `// bits in the binary representation of ` ` ` `// all the numbers from 1 to n ` ` ` `static` `int` `countUnsetBits(` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the count of unset bits ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `// For every integer from the range [1, n] ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// A copy of the current integer ` ` ` `int` `temp = i; ` ` ` ` ` `// Count of unset bits in ` ` ` `// the current integer ` ` ` `while` `(temp > ` `0` `) ` ` ` `{ ` ` ` ` ` `// If current bit is unset ` ` ` `if` `(temp % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `cnt = cnt + ` `1` `; ` ` ` `} ` ` ` ` ` `temp = temp / ` `2` `; ` ` ` `} ` ` ` `} ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(countUnsetBits(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of unset ` `# bits in the binary representation of ` `# all the numbers from 1 to n ` `def` `countUnsetBits(n) : ` ` ` ` ` `# To store the count of unset bits ` ` ` `cnt ` `=` `0` `; ` ` ` ` ` `# For every integer from the range [1, n] ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `) : ` ` ` ` ` `# A copy of the current integer ` ` ` `temp ` `=` `i; ` ` ` ` ` `# Count of unset bits in ` ` ` `# the current integer ` ` ` `while` `(temp) : ` ` ` ` ` `# If current bit is unset ` ` ` `if` `(temp ` `%` `2` `=` `=` `0` `) : ` ` ` `cnt ` `+` `=` `1` `; ` ` ` ` ` `temp ` `=` `temp ` `/` `/` `2` `; ` ` ` ` ` `return` `cnt; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` `; ` ` ` ` ` `print` `(countUnsetBits(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count of unset ` ` ` `// bits in the binary representation of ` ` ` `// all the numbers from 1 to n ` ` ` `static` `int` `countUnsetBits(` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the count of unset bits ` ` ` `int` `cnt = 0; ` ` ` ` ` `// For every integer from the range [1, n] ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// A copy of the current integer ` ` ` `int` `temp = i; ` ` ` ` ` `// Count of unset bits in ` ` ` `// the current integer ` ` ` `while` `(temp > 0) ` ` ` `{ ` ` ` ` ` `// If current bit is unset ` ` ` `if` `(temp % 2 == 0) ` ` ` `cnt = cnt + 1; ` ` ` ` ` `temp = temp / 2; ` ` ` `} ` ` ` `} ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.Write(countUnsetBits(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count total set bits in all numbers from 1 to N | Set 3
- Count total set bits in all numbers from 1 to n
- Count total set bits in all numbers from 1 to n | Set 2
- Python map function | Count total set bits in all numbers from 1 to n
- Count unset bits of a number
- Count unset bits in a range
- Python | Count unset bits in a range
- Count total bits in a number
- Count of total bits toggled/flipped in binary representation of 0 to N
- Unset the last m bits
- Unset bits in the given range
- Check whether all the bits are unset in the given range or not
- Check whether all the bits are unset in the given range
- Count of total subarrays whose sum is a Fibonacci Numbers
- Find the largest number with n set and m unset bits
- Find the smallest number with n set and m unset bits
- Find the total count of numbers up to N digits in a given base B
- Count of numbers whose 0th and Nth bits are set
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count set bits in the Kth number after segregating even and odd from N natural numbers

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.