truncPow2

Gives the last power of two before val. $(T) can be any built-in numerical type.

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

Parameters

val T

any number

Return Value

Type: T

the last power of two before val

Examples

1 assert(truncPow2(3) == 2);
2 assert(truncPow2(4) == 4);
3 assert(truncPow2(10) == 8);
4 assert(truncPow2(4000) == 2048);
5 
6 assert(truncPow2(-5) == -4);
7 assert(truncPow2(-20) == -16);
8 
9 assert(truncPow2(uint.max) == int.max + 1);
10 assert(truncPow2(uint.min) == 0);
11 assert(truncPow2(ulong.max) == long.max + 1);
12 assert(truncPow2(ulong.min) == 0);
13 
14 assert(truncPow2(int.max) == (int.max / 2) + 1);
15 assert(truncPow2(int.min) == int.min);
16 assert(truncPow2(long.max) == (long.max / 2) + 1);
17 assert(truncPow2(long.min) == long.min);
assert(truncPow2(2.1) == 2.0);
assert(truncPow2(7.0) == 4.0);
assert(truncPow2(-1.9) == -1.0);
assert(truncPow2(0.24) == 0.125);
assert(truncPow2(-7.0) == -4.0);

assert(truncPow2(double.infinity) == double.infinity);

Meta

Suggestion Box / Bug Report