[Solved] Check if number is divisible by a power of two [duplicate]


In the Long class, there is a handy static function numberOfTrailingZeros, which does almost what you want, except that it returns zero (instead of -1) when the input is not divisible by 2. You could handle that case in different ways. For example, extending the answer of @0x476f72616e

if ((num & 0x1) == 0)
    return Long.numberOfTrailingZeros(num);
else
    return -1;

7

solved Check if number is divisible by a power of two [duplicate]