Re:Write A Function To Determine If A Number Is A Power Of 2
by nevinera on Monday October 18, 2010 @12:24PM
(#1804073)
mod(x,2) will tell you that it's even, not that it's a power of two.
[ Parent
| on HN ]
Re:Write A Function To Determine If A Number Is A Power Of 2
by Retric on Monday October 18, 2010 @12:41PM
(#1804157)
I think his first example was a recursive function like:
if !(mod(x,2) {
if (x == 1) return true else return false;
}else return is_power_of_2(x/2);
or if !(mod(x,2) {
if (x == 1) return true;
if ((x > 1) || (x == 0)) return false;
return is_power_of_2(x * 2);
}else if (x > 2) return is_power_of_2(x/2) else return false;
PS: 2^0 = 1 and 2^-1 so you could have 3 versions of this fuction.