smartOp.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(N left, M exp)
    template smartOp(IntFlagPolicy policy)
    pure @safe nothrow @nogc
    modPow2
    (
    N
    M
    )
    (
    const N left
    ,
    const M exp
    )
    if (
    (
    isFloatingPoint!N &&
    isScalarType!M
    )
    ||
    (
    isScalarType!N &&
    isFloatingPoint!M
    )
    )
  2. auto modPow2(N left, M exp)

Examples

import checkedint.sticky : smartOp; // use IntFlagPolicy.sticky

assert(smartOp.modPow2( 101,  1) ==  1);
assert(smartOp.modPow2( 101,  3) ==  5);
assert(smartOp.modPow2(-101,  3) == -5);

assert(smartOp.modPow2(101, -2) ==  0);
assert(smartOp.modPow2(101, 1_000) == 101);

Meta