safeOp.divPow2

Equivalent to left / pow(2, exp), but faster and works with a wider range of inputs. This is a safer alternative to left >> exp that is still very fast.

Note that (conceptually) rounding occurs after the /, meaning that divPow2(left, -exp) is equivalent to mulPow2(left, exp).

  1. auto divPow2(N left, M exp)
  2. auto divPow2(N left, M exp)
    template safeOp(IntFlagPolicy policy)
    @safe
    divPow2
    (
    N
    M
    )
    (
    const N left
    ,
    const M exp
    )

Examples

import checkedint.noex : safeOp; // use IntFlagPolicy.noex

assert(safeOp.divPow2(65536, 8) == 256);
assert(safeOp.divPow2(-100, 100) == 0);
assert(safeOp.divPow2(-23, -5) == -736);

safeOp.divPow2(10_000_000, -10);
assert(IntFlags.local.clear() == IntFlag.posOver);

Meta