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

1 assert(nextPow2(2) == 4);
2 assert(nextPow2(10) == 16);
3 assert(nextPow2(4000) == 4096);
4 
5 assert(nextPow2(-2) == -4);
6 assert(nextPow2(-10) == -16);
7 
8 assert(nextPow2(uint.max) == 0);
9 assert(nextPow2(uint.min) == 0);
10 assert(nextPow2(size_t.max) == 0);
11 assert(nextPow2(size_t.min) == 0);
12 
13 assert(nextPow2(int.max) == 0);
14 assert(nextPow2(int.min) == 0);
15 assert(nextPow2(long.max) == 0);
16 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

Suggestion Box / Bug Report