Maximize the number by rearranging bits | An efficient Java 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 along 32 bits are set . ; find a number witn n least significant set bits . ; Now shift result by 32 - n ; Driver Code
import java . io . * ; class GFG { static long _popcnt32 ( long n ) { long count = 0 ; while ( n != 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static long maximize ( long a ) { long n = _popcnt32 ( a ) ; if ( n == 32 ) return a ; long res = ( 1 << n ) - 1 ; return ( res << ( 32 - n ) ) ; } public static void main ( String args [ ] ) { long a = 3 ; System . out . print ( maximize ( a ) ) ; } }