Skip to content

Modern Math IsPow2

Paul Squires edited this page Oct 19, 2026 · 5 revisions

Home › Math Library › Math Library › IsPow2

IsPow2

Returns true if the value is a power of two; zero is not one.

Syntax

IsPow2( byval x as T ) as boolean

Notes

  • Zero is not, which is the case a hand-written ( x and ( x - 1 ) ) = 0 gets wrong.
  • One is: 2^0.
  • The test behind an alignment check and a hash-table capacity assertion.

Examples

#import "std/numeric"
using numeric

dim as uint32 a = 8
print IsPow2( a )            '' true

dim as uint32 b = 6
print IsPow2( b )            '' false

'' ZERO IS NOT a power of two -- which the usual one-liner gets wrong
dim as uint32 zero_ = 0
print IsPow2( zero_ )        '' false
print ( ( zero_ and ( zero_ - 1 ) ) = 0 )     '' -1 -- the wrong answer

Library: Math Library
Category: Math Library
Documented under: Members

Clone this wiki locally