Count of total bits toggled / flipped in binary representation of 0 to N | Java 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
class GFG { static void solve ( int N ) { int ans = 0 ; while ( N != 0 ) { ans += N ; N /= 2 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 5 ; solve ( N ) ; } }