The OpenD Programming Language

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

  assert(truncPow2(3) == 2);
  assert(truncPow2(4) == 4);
  assert(truncPow2(10) == 8);
  assert(truncPow2(4000) == 2048);

  assert(truncPow2(-5) == -4);
  assert(truncPow2(-20) == -16);

  assert(truncPow2(uint.max) == int.max + 1);
  assert(truncPow2(uint.min) == 0);
  assert(truncPow2(ulong.max) == long.max + 1);
  assert(truncPow2(ulong.min) == 0);

  assert(truncPow2(int.max) == (int.max / 2) + 1);
version (LDC)
{
  // this test relies on undefined behaviour, i.e. (1 << 63) == int.min
  // that fails for LDC with optimizations enabled
}
else
{
  assert(truncPow2(int.min) == int.min);
}
  assert(truncPow2(long.max) == (long.max / 2) + 1);
  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