Last Updated: February 25, 2016
·
495
· cerebrate

Integer raise-to-power-of

Don't cast back and forth to double just to use Math.Pow(). Seriously.

public static int Exp(this int x, int y)
{
    int result = 1;
    while (y > 0)
    {
        if ((y & 1) != 0)
        {
            result *= x;
        }
        y >>= 1;
        x *= x;
    }
    return result;
}