nextPow2

Gives the next power of two after val. T can be any built-in numerical type.

If the operation would lead to an over/underflow, this function will return 0.

  1. T nextPow2(T val)
    T
    nextPow2
    (
    T
    )
    (
    const T val
    )
  2. T nextPow2(T val)

Parameters

val T

any number

Return Value

Type: T

the next power of two after val

Examples

assert(nextPow2(2) == 4);
assert(nextPow2(10) == 16);
assert(nextPow2(4000) == 4096);

assert(nextPow2(-2) == -4);
assert(nextPow2(-10) == -16);

assert(nextPow2(uint.max) == 0);
assert(nextPow2(uint.min) == 0);
assert(nextPow2(size_t.max) == 0);
assert(nextPow2(size_t.min) == 0);

assert(nextPow2(int.max) == 0);
assert(nextPow2(int.min) == 0);
assert(nextPow2(long.max) == 0);
assert(nextPow2(long.min) == 0);
assert(nextPow2(2.1) == 4.0);
assert(nextPow2(-2.0) == -4.0);
assert(nextPow2(0.25) == 0.5);
assert(nextPow2(-4.0) == -8.0);

assert(nextPow2(double.max) == 0.0);
assert(nextPow2(double.infinity) == double.infinity);

Meta