I am looking at http://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int.
This is the answer they got.
I am trying to make it work for C# but I am getting comparing int to bool and all this other stuff. . . and I can't figure out why they are comparing & 1 wouldn't that mean and true? What is the point of that. It doesn't seem efficient.
int ipow(int base, int exp)
{
int result = 1;
while (exp)
{
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
I am doing exp == on the compare but that 1 is still there and I don't know if I need it.
Does someone know what the 1 in "if (exp & 1) " is for? Or if I need it? I can't see the use.