On Oct 17, 4:58 pm, Ixiaus <parnel...@comcast.netwrote:
Thank you for the quick responses.
I did not know that about integer literals beginning with a '0', so
thank you for the explanation. I never really use PHP except for
handling basic forms and silly web stuff, this is why I picked up
Python because I want to teach myself a more powerful and broad
programming language.
With regard to why I asked: I wanted to learn about Binary math in
conjunction with Python, so I wrote a small function that would return
a base 10 number from a binary number. It is nice to know about the
int() function now.
Just for the sake of it, this was the function I came up with:
def bin2dec(val):
li = list(val)
li.reverse()
res = [int(li[x])*2**x for x in range(len(li))]
res.reverse()
print sum(res)
Now that I look at it, I probably don't need that last reverse()
because addition is commutative...
def bin2dec(val):
li = list(val)
li.reverse()
res = [int(li[x])*2**x for x in range(len(li))]
print sum(res)
It basically does the same thing int(string, 2) does.
Thank you for the responses!
You could also get ahold of the gmpy module. You get conversion
to binary and also some useful other binary functions as shown
below:
# the Collatz Conjecture in binary
import gmpy
n = 27
print '%4d %s' % (n,gmpy.digits(n,2).zfill(16))
sv = [] # sequence vector, blocks of contiguous LS 0's
while n != 1:
old_n = n
n = 3*n + 1 # result always even
f = gmpy.scan1(n,0) # find least significant 1 bit
n >>= f # remove LS 0's in one fell swoop
sv.append(f) # record f sequence
PopC = gmpy.popcount(n) # count of 1 bits
HamD = gmpy.hamdist(n,old_n) # bits changed
print '%4d %s' % (n,gmpy.digits(n,2).zfill(16)),
print 'PopC:%2d HamD:%2d' % (PopC,HamD)
print sv
## 27 0000000000011011
## 41 0000000000101001 PopC: 3 HamD: 3
## 31 0000000000011111 PopC: 5 HamD: 4
## 47 0000000000101111 PopC: 5 HamD: 2
## 71 0000000001000111 PopC: 4 HamD: 3
## 107 0000000001101011 PopC: 5 HamD: 3
## 161 0000000010100001 PopC: 3 HamD: 4
## 121 0000000001111001 PopC: 5 HamD: 4
## 91 0000000001011011 PopC: 5 HamD: 2
## 137 0000000010001001 PopC: 3 HamD: 4
## 103 0000000001100111 PopC: 5 HamD: 6
## 155 0000000010011011 PopC: 5 HamD: 6
## 233 0000000011101001 PopC: 5 HamD: 4
## 175 0000000010101111 PopC: 6 HamD: 3
## 263 0000000100000111 PopC: 4 HamD: 4
## 395 0000000110001011 PopC: 5 HamD: 3
## 593 0000001001010001 PopC: 4 HamD: 7
## 445 0000000110111101 PopC: 7 HamD: 7
## 167 0000000010100111 PopC: 5 HamD: 4
## 251 0000000011111011 PopC: 7 HamD: 4
## 377 0000000101111001 PopC: 6 HamD: 3
## 283 0000000100011011 PopC: 5 HamD: 3
## 425 0000000110101001 PopC: 5 HamD: 4
## 319 0000000100111111 PopC: 7 HamD: 4
## 479 0000000111011111 PopC: 8 HamD: 3
## 719 0000001011001111 PopC: 7 HamD: 3
##1079 0000010000110111 PopC: 6 HamD: 7
##1619 0000011001010011 PopC: 6 HamD: 4
##2429 0000100101111101 PopC: 8 HamD: 8
## 911 0000001110001111 PopC: 7 HamD: 7
##1367 0000010101010111 PopC: 7 HamD: 6
##2051 0000100000000011 PopC: 3 HamD: 6
##3077 0000110000000101 PopC: 4 HamD: 3
## 577 0000001001000001 PopC: 3 HamD: 5
## 433 0000000110110001 PopC: 5 HamD: 6
## 325 0000000101000101 PopC: 4 HamD: 5
## 61 0000000000111101 PopC: 5 HamD: 5
## 23 0000000000010111 PopC: 4 HamD: 3
## 35 0000000000100011 PopC: 3 HamD: 3
## 53 0000000000110101 PopC: 4 HamD: 3
## 5 0000000000000101 PopC: 2 HamD: 2
## 1 0000000000000001 PopC: 1 HamD: 1
##[1, 2, 1, 1, 1, 1, 2, 2, 1, 2, 1, 1, 2,
## 1, 1, 1, 2, 3, 1, 1, 2, 1, 2, 1, 1, 1,
## 1, 1, 3, 1, 1, 1, 4, 2, 2, 4, 3, 1, 1,
## 5, 4]