Given a positive integer **N**, the task is to count the total number of set bits in binary representation of all the numbers from **1** to **N**.

**Examples:**

Input:N = 3

Output:4

setBits(1) + setBits(2) + setBits(3) = 1 + 1 + 2 = 4

Input:N = 6

Output:9

**Approach:** Solution to this problem has been published in the Set 1 and the Set 2 of this article. Here, a dynamic programming based approach is discussed.

**Base case:**Number of set bits in 0 and 1 are 0 and 1 respectively.- Now for every element
**i**from the range**[2, N]**, if**i is even**then it will have the same number of set bits as**i / 2**because to get the number**i**we just shift the number**i / 2**by one. While shifting, the number of set bits does not change. - Similarly, if
**i is odd**then it will have**1**additional set bit at**0**position than^{th}**i – 1**which was even.

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 ` `// set bits in all the integers ` `// from the range [1, n] ` `int` `countSetBits(` `int` `n) ` `{ ` ` ` ` ` `// To store the required count ` ` ` `// of the set bits ` ` ` `int` `cnt = 0; ` ` ` ` ` `// To store the count of set ` ` ` `// bits in every integer ` ` ` `vector<` `int` `> setBits(n + 1); ` ` ` ` ` `// 0 has no set bit ` ` ` `setBits[0] = 0; ` ` ` ` ` `// 1 has a single set bit ` ` ` `setBits[1] = 1; ` ` ` ` ` `// For the rest of the elements ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` ` ` `// If current element i is even then ` ` ` `// it has set bits equal to the count ` ` ` `// of the set bits in i / 2 ` ` ` `if` `(i % 2 == 0) { ` ` ` `setBits[i] = setBits[i / 2]; ` ` ` `} ` ` ` ` ` `// Else it has set bits equal to one ` ` ` `// more than the previous element ` ` ` `else` `{ ` ` ` `setBits[i] = setBits[i - 1] + 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Sum all the set bits ` ` ` `for` `(` `int` `i = 0; i <= n; i++) { ` ` ` `cnt = cnt + setBits[i]; ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `cout << countSetBits(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// set bits in all the integers ` `// from the range [1, n] ` `static` `int` `countSetBits(` `int` `n) ` `{ ` ` ` ` ` `// To store the required count ` ` ` `// of the set bits ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `// To store the count of set ` ` ` `// bits in every integer ` ` ` `int` `[]setBits = ` `new` `int` `[n + ` `1` `]; ` ` ` ` ` `// 0 has no set bit ` ` ` `setBits[` `0` `] = ` `0` `; ` ` ` ` ` `// 1 has a single set bit ` ` ` `setBits[` `1` `] = ` `1` `; ` ` ` ` ` `// For the rest of the elements ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// If current element i is even then ` ` ` `// it has set bits equal to the count ` ` ` `// of the set bits in i / 2 ` ` ` `if` `(i % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `setBits[i] = setBits[i / ` `2` `]; ` ` ` `} ` ` ` ` ` `// Else it has set bits equal to one ` ` ` `// more than the previous element ` ` ` `else` ` ` `{ ` ` ` `setBits[i] = setBits[i - ` `1` `] + ` `1` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Sum all the set bits ` ` ` `for` `(` `int` `i = ` `0` `; i <= n; i++) ` ` ` `{ ` ` ` `cnt = cnt + setBits[i]; ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `6` `; ` ` ` ` ` `System.out.println(countSetBits(n)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of ` `# set bits in all the integers ` `# from the range [1, n] ` `def` `countSetBits(n): ` ` ` ` ` `# To store the required count ` ` ` `# of the set bits ` ` ` `cnt ` `=` `0` ` ` ` ` `# To store the count of set ` ` ` `# bits in every integer ` ` ` `setBits ` `=` `[` `0` `for` `x ` `in` `range` `(n ` `+` `1` `)] ` ` ` ` ` `# 0 has no set bit ` ` ` `setBits[` `0` `] ` `=` `0` ` ` ` ` `# 1 has a single set bit ` ` ` `setBits[` `1` `] ` `=` `1` ` ` ` ` `# For the rest of the elements ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` ` ` `# If current element i is even then ` ` ` `# it has set bits equal to the count ` ` ` `# of the set bits in i / 2 ` ` ` `if` `(i ` `%` `2` `=` `=` `0` `): ` ` ` `setBits[i] ` `=` `setBits[i ` `/` `/` `2` `] ` ` ` ` ` `# Else it has set bits equal to one ` ` ` `# more than the previous element ` ` ` `else` `: ` ` ` `setBits[i] ` `=` `setBits[i ` `-` `1` `] ` `+` `1` ` ` ` ` `# Sum all the set bits ` ` ` `for` `i ` `in` `range` `(` `0` `, n ` `+` `1` `): ` ` ` `cnt ` `=` `cnt ` `+` `setBits[i] ` ` ` ` ` `return` `cnt ` ` ` `# Driver code ` `n ` `=` `6` `print` `(countSetBits(n)) ` ` ` `# This code is contributed by Sanjit Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count of ` ` ` `// set bits in all the integers ` ` ` `// from the range [1, n] ` ` ` `static` `int` `countSetBits(` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the required count ` ` ` `// of the set bits ` ` ` `int` `cnt = 0; ` ` ` ` ` `// To store the count of set ` ` ` `// bits in every integer ` ` ` `int` `[]setBits = ` `new` `int` `[n + 1]; ` ` ` ` ` `// 0 has no set bit ` ` ` `setBits[0] = 0; ` ` ` ` ` `// 1 has a single set bit ` ` ` `setBits[1] = 1; ` ` ` ` ` `// For the rest of the elements ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// If current element i is even then ` ` ` `// it has set bits equal to the count ` ` ` `// of the set bits in i / 2 ` ` ` `if` `(i % 2 == 0) ` ` ` `{ ` ` ` `setBits[i] = setBits[i / 2]; ` ` ` `} ` ` ` ` ` `// Else it has set bits equal to one ` ` ` `// more than the previous element ` ` ` `else` ` ` `{ ` ` ` `setBits[i] = setBits[i - 1] + 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Sum all the set bits ` ` ` `for` `(` `int` `i = 0; i <= n; i++) ` ` ` `{ ` ` ` `cnt = cnt + setBits[i]; ` ` ` `} ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `Console.WriteLine(countSetBits(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

9

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 2
- Count total set bits in all numbers from 1 to n
- Python map function | Count total set bits in all numbers from 1 to n
- Count total set bits in all numbers from range L to R
- Count total unset bits in all the numbers from 1 to N
- Count of total bits toggled/flipped in binary representation of 0 to N
- Count total bits in a number
- Print numbers having first and last bits as the only set bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Check if all bits can be made same by flipping two consecutive bits
- Toggle bits of a number except first and last bits
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Find the total count of numbers up to N digits in a given base B
- Find all combinations of k-bit numbers with n bits set where 1 <= n <= k in sorted order
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Count numbers in range [L, R] having K consecutive set bits
- Count set bits in Bitwise XOR of all adjacent elements upto N
- Count positions in Binary Matrix having equal count of set bits in corresponding row and column
- Count all pairs of an array which differ in K bits

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.