safeOp.modPow2

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

  1. auto modPow2(const N left, const M exp)
  2. auto modPow2(const N left, const M exp)
    template safeOp(IntFlagPolicy policy)
    modPow2
    pure @safe nothrow @nogc
    (
    N
    M
    )
    (
    const N left
    ,
    const M exp
    )
    if (
    isFixedPoint!N &&
    isFixedPoint!M
    )

Examples

1 import checkedint.sticky : safeOp; // use IntFlagPolicy.sticky
2 
3 assert(safeOp.modPow2( 101,  1) ==  1);
4 assert(safeOp.modPow2( 101,  3) ==  5);
5 assert(safeOp.modPow2(-101,  3) == -5);
6 
7 assert(safeOp.modPow2(101, -2) ==  0);
8 assert(safeOp.modPow2(101, 1_000) == 101);

Meta