07-02-2003, 02:42 AM

Here's a rather complex one-liner to determine if a given number N is a power of 2.

Note: My original code did not add the .5 to round the result. However, further testing showed that for certain values of N, like 128, the arithmetic failed. Can anyone tell me why 128 fails without the rounding?

*****

Code:

`defLNG a-z`

if 2^(int(log(N)/log(2)+.5)) = N then print n;" is a power of 2"

Note: My original code did not add the .5 to round the result. However, further testing showed that for certain values of N, like 128, the arithmetic failed. Can anyone tell me why 128 fails without the rounding?

*****