Count total set bits in all numbers from 1 to n

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

Examples:

Input: n = 3
Output:  4

Input: n = 6
Output: 9

Input: n = 7
Output: 12

Input: n = 8
Output: 13

Source: Amazon Interview Question

Method 1 (Simple)
A simple solution is to run a loop from 1 to n and sum the count of set bits in all numbers from 1 to n.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple program to count set bits
// in all numbers from 1 to n.
#include <stdio.h>
  
// A utility function to count set bits
// in a number x
unsigned int countSetBitsUtil(unsigned int x);
  
// Returns count of set bits present in all
// numbers from 1 to n
unsigned int countSetBits(unsigned int n)
{
    int bitCount = 0; // initialize the result
  
    for (int i = 1; i <= n; i++)
        bitCount += countSetBitsUtil(i);
  
    return bitCount;
}
  
// A utility function to count set bits 
// in a number x
unsigned int countSetBitsUtil(unsigned int x)
{
    if (x <= 0)
        return 0;
    return (x % 2 == 0 ? 0 : 1) + countSetBitsUtil(x / 2);
}
  
// Driver program to test above functions
int main()
{
    int n = 4;
    printf("Total set bit count is %d", countSetBits(n));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple program to count set bits
// in all numbers from 1 to n.
  
class GFG{
  
    // Returns count of set bits present
    //  in all numbers from 1 to n
    static int countSetBits( int n)
    {
        // initialize the result
        int bitCount = 0;
      
        for (int i = 1; i <= n; i++)
            bitCount += countSetBitsUtil(i);
      
        return bitCount;
    }
      
    // A utility function to count set bits 
    // in a number x
    static int countSetBitsUtil( int x)
    {
        if (x <= 0)
            return 0;
        return (x % 2 == 0 ? 0 : 1) + 
               countSetBitsUtil(x / 2);
    }
      
    // Driver program 
    public static void main(String[] args)
    {
        int n = 4;
        System.out.print("Total set bit count is ");
        System.out.print(countSetBits(n));
    }
}
  
// This code is contributed by
// Smitha Dinesh Semwal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A simple program to count set bits
# in all numbers from 1 to n.
  
# Returns count of set bits present in all
# numbers from 1 to n
def countSetBits(n):
      
    # initialize the result
    bitCount = 0 
  
    for i in range(1, n + 1):
        bitCount += countSetBitsUtil(i)
  
    return bitCount
  
  
# A utility function to count set bits 
# in a number x
def countSetBitsUtil(x):
  
    if (x <= 0):
        return 0
    return (0 if int(x % 2) == 0 else 1) +  countSetBitsUtil(int(x / 2))
  
  
# Driver program
if __name__=='__main__'
    n = 4
    print("Total set bit count is", countSetBits(n))
       
# This code is contributed by
# Smitha Dinesh Semwal    

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple C# program to count set bits
// in all numbers from 1 to n.
using System;
  
class GFG
{
    // Returns count of set bits present
    // in all numbers from 1 to n
    static int countSetBits( int n)
    {
        // initialize the result
        int bitCount = 0;
      
        for (int i = 1; i <= n; i++)
            bitCount += countSetBitsUtil(i);
      
        return bitCount;
    }
      
    // A utility function to count set bits 
    // in a number x
    static int countSetBitsUtil( int x)
    {
        if (x <= 0)
            return 0;
        return (x % 2 == 0 ? 0 : 1) + 
            countSetBitsUtil(x / 2);
    }
      
    // Driver program 
    public static void Main()
    {
        int n = 4;
        Console.Write("Total set bit count is ");
        Console.Write(countSetBits(n));
    }
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// A simple program to count set bits
// in all numbers from 1 to n.
  
// Returns count of set bits present 
// in all numbers from 1 to n
function countSetBits($n)
{
    $bitCount = 0; // initialize the result
  
    for ($i = 1; $i <= $n; $i++)
        $bitCount += countSetBitsUtil($i);
  
    return $bitCount;
}
  
// A utility function to count 
// set bits in a number x
function countSetBitsUtil($x)
{
    if ($x <= 0)
        return 0;
    return ($x % 2 == 0 ? 0 : 1) + 
            countSetBitsUtil($x / 2);
}
  
// Driver Code
$n = 4;
echo "Total set bit count is "
               countSetBits($n);
  
// This code is contributed by ChitraNayal
?>

chevron_right