Maximize the number by rearranging bits | An efficient C ++ program to find minimum number formed by bits of a given number . ; Returns maximum number formed by bits of a given number . ; _popcnt32 ( a ) gives number of 1 's  present in binary representation of a. ; If all 32 bits are set . ; find a number witn n least significant set bits . ; Now shift result by 32 - n ; Driver function .
#include <bits/stdc++.h> NEW_LINE #define ll  unsigned int NEW_LINE using namespace std ; ll maximize ( ll a ) { ll n = _popcnt32 ( a ) ; if ( n == 32 ) return a ; ll res = ( 1 << n ) - 1 ; return ( res << ( 32 - n ) ) ; } int main ( ) { ll a = 3 ; cout << maximize ( a ) << endl ; return 0 ; }