isPowerOfTwo

Share your advanced PureBasic knowledge/code with the community.
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

isPowerOfTwo

Post by benny »

Code updated For 5.20+

Maybe someone finds any use in the following procedure (found somewhere
on the net).

The procedure returns TRUE if x.l is a power of 2 and FALSE if it
is not.

Code: Select all

Procedure isPowerOfTwo(x)
  If ((x & -x) = x)
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

Debug isPowerOfTwo(64)
Debug isPowerOfTwo(6)

regards,
benny!
-
pe0ple ar3 str4nge!!!