Powers of 2 to required sum | CPP program to find the blocks for given number . ; Converting the decimal number into its binary equivalent . ; Displaying the output when the bit is '1' in binary equivalent of number . ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void block ( long int x ) { vector < long int > v ; cout << " Blocks ▁ for ▁ " << x << " ▁ : ▁ " ; while ( x > 0 ) { v . push_back ( x % 2 ) ; x = x / 2 ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v [ i ] == 1 ) { cout << i ; if ( i != v . size ( ) - 1 ) cout << " , ▁ " ; } } cout << endl ; } int main ( ) { block ( 71307 ) ; block ( 1213 ) ; block ( 29 ) ; block ( 100 ) ; return 0 ; }