Skip to content

Modern Math NextPow2

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

Home › Math Library › Math Library › NextPow2

NextPow2

Returns the smallest power of two at or above the value; zero gives 1.

Syntax

NextPow2( byval x as T ) as T

Notes

  • At or above: a value that is already a power of two comes back unchanged, which is what a capacity computation wants.
  • Zero gives 1.
  • A value with no power of two above it in the type saturates rather than wrapping to zero.

Examples

#import "std/numeric"
using numeric

dim as uint32 a = 5
print NextPow2( a )          '' 8

'' Already a power of two, so it does not move
dim as uint32 b = 8
print NextPow2( b )          '' 8

dim as uint32 zero_ = 0
print NextPow2( zero_ )      '' 1

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

Clone this wiki locally