Count of total bits toggled / flipped in binary representation of 0 to N | C ++ program to count the number of toggles required to generate all numbers from 0 to N ; Function to count and print the required number of toggles ; Store the count of toggles ; Add the contribution of the current LSB ; Update N ; Print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; void solve ( ll N ) { ll ans = 0 ; while ( N != 0 ) { ans += N ; N /= 2 ; } cout << ans << endl ; } int main ( ) { ll N = 5 ; solve ( N ) ; return 0 ; }