我的代码是测试,我试图弄清楚为什么不测试 2 的指数。无论如何,由于某种原因p & ~(p & (p - 1))
,解释器不喜欢在 while 循环之前。in = 1
代码:
def getnumber(str):
num=0
for i in str:
zz = ord(i)-48
if zz<0 or zz>9:
num=-1
break
else:
num=(num*10)+zz
return num
def testexp2(p):
table = {1:1,2:2,4:3,8:4,16:5,32:6,64:7,128:8,256:9}
if p & ~(p & (p - 1)):
print "yes"
else:
print "no"
in = 1
while in is not -1:
in = raw_input("> ")
in = getnumber(in)
if in>-1:
testexp2(in)
else:
print "\nDone\n\n"