def isPowerOf2(num): return num == 1 or num == 2 or (num%2 == 0 and isPowerOf2(num/2))
def isPowerOf2(num): count = 0 while(num > 0): count += num%2 num >>= 1 return count == 1
Update: From David, in the comments:
def isPowerOf2(num): return (num & (num - 1) == 0)
I've never gotten to do enough bit twiddling to have let me think of that.
4by12 » Two Envelopes. I don't get it, I'll have to look over it again later.
Infomole - a friend's project. In Ruby on Rails!
RSS feed Atom feed Weblog archive Recent comments on 5 posts
new⇒Court rejects death penalty for raping children - Yahoo! News
:)...
Keith: Jul 4, 11:32am
Girls, please don't get breast implants
> And no, you will not be receivinga picture. :-(...
Keith: Jul 2, 6:05am
Javascript clone function
This is a clever way to clone anobject if you are using YAHOO UI.Same tec...
Antonio: Jul 1, 12:47pm
I hate Norton Antivirus
Oh just one other thing norton isgreat at keeping people out of yourcompu...
kevin.sands: Jul 1, 12:50am
Terminator 3 was awful
I think the biggest reason why T3totally blew was because EdwardFurlong g...
76.167.172.64: Jun 29, 3:06am
Generated in about 0.046s.
(Used 7 db queries)
new⇒Court rejects death penalty for raping children - Yahoo! News
:)...
Keith: Jul 4, 11:32am