Ceil function is used for negative numbers as - 1 > - 4. It would be opposite to positive numbers where 1 < 4
int pos = ( int ) Math . ceil ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ; return ( int ) ( - 1 * Math . pow ( 2 , pos ) ) ; }