Given an integer N, the task is to find the total number of bits toggled to obtain all numbers from 0 to N sequentially.
Examples:
Input: N = 5
Output: 8
Explanation:
Let’s represent numbers from 0 to 5 in binary:
000 > 001 : 1 bit toggled
001 > 010 : 2 bits toggled
010 > 011 : 1 bit toggled
011 > 100 : 3 bits toggled
100 > 101 : 1 bit toggled
Hence, 8 bits toggledInput: N = 1
Output: 1
Approach:
Follow the steps below to solve the problems:

The following observations need to be made to solve the problem:
The rightmost bit toggles every time.
(000) > (001) > (010) > (011)
So, the contribution of this bit to the count will be N.
The next bit toggles after every 2 numbers.
(000) > (001) > (010) > (011) > (100)
Hence, the contribution of this bit to the count will be N/2.  Thus, we can conclude that the i^{ th} least significant bit will contribute N/(2^{ i}) to the count of toggles.
 Hence, the sum of N/(2^{i}) where i is in the range [0, log_{2}N], gives the required answer.
 Hence, initialize a variable ans. Add N to ans and update N to N/2. Repeat this process until N becomes 0, to get the final result.
 Prime Number of Set Bits in Binary Representation  Set 2
 Check if all the set bits of the binary representation of N are at least K places away
 Prime Number of Set Bits in Binary Representation  Set 1
 Number of mismatching bits in the binary representation of two integers
 Count total bits in a number
 Count total set bits in all numbers from 1 to n  Set 2
 Count total set bits in all numbers from 1 to n
 Count total set bits in all numbers from 1 to N  Set 3
 Count total unset bits in all the numbers from 1 to N
 Python map function  Count total set bits in all numbers from 1 to n
 Count numbers have all 1s together in binary representation
 C program to count zeros and ones in binary representation of a number
 Maximize count of set bits in a root to leaf path in a binary tree
 Maximum number of set bits count in a Ksize substring of a Binary String
 Count number of trailing zeros in Binary representation of a number using Bitset
 Find the occurrence of the given binary pattern in the binary representation of the array elements
 Check if bits of a number has count of consecutive set bits in increasing order
 Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
 Binary representation of next number
 Find value of kth bit in binary representation
Below is the implementation of the above approach:
C++
// C++ program to count // the number of toggles // required to generate // all numbers from 0 to N #include <bits/stdc++.h> using namespace std; typedef long long int ll; // Function to count and print // the required number of // toggles void solve(ll N) { // Store the count // of toggles ll ans = 0; while (N != 0) { // Add the contribution // of the current LSB ans += N; // Update N N /= 2; } // Print the result cout << ans << endl; } // Driver code int main() { ll N = 5; solve(N); return 0; } 
Java
// Java program to count the // number of toggles required // to generate all numbers // from 0 to N class GFG{ // Function to count and print // the required number of // toggles static void solve( int N) { // Store the count // of toggles int ans = 0 ; while (N != 0 ) { // Add the contribution // of the current LSB ans += N; // Update N N /= 2 ; } // Print the result System.out.println(ans); } // Driver code public static void main(String []args) { int N = 5 ; solve(N); } } // This code is contributed by Ritik Bansal 
8
Time Complexity: O(log N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a studentfriendly price and become industry ready.
Recommended Posts:
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.
Improved By : btc_148