KBD

Keith Devens .com

Friday, July 4, 2008 Flag waving
Follow the path. – me

Archive: May 31, 2006

← May 30, 2006June 01, 2006 →

Daily link icon Wednesday, May 31, 2006

A couple of solutions...

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.

  1. 4by12 » Two Envelopes. I don't get it, I'll have to look over it again later.

       (0) Tags: [Riddles]
  2. Infomole - a friend's project. In Ruby on Rails! Smiley

       (0)
← May 30, 2006June 01, 2006 →
July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

Girls, please don't get breast implants

> And no, you will not be receiving​a picture.

:-(...

Keith: Jul 2, 6:05am

Javascript clone function

This is a clever way to clone an​object if you are using YAHOO UI.​Same tec...

Antonio: Jul 1, 12:47pm

I hate Norton Antivirus

Oh just one other thing norton is​great at keeping people out of your​compu...

kevin.sands: Jul 1, 12:50am

Terminator 3 was awful

I think the biggest reason why T3​totally blew was because Edward​Furlong g...

76.167.172.64: Jun 29, 3:06am

Generated in about 0.046s.

(Used 7 db queries)

mobile phone