On Oct 17, 4:58 pm, Ixiaus <parnel...@comc ast.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 000000000001101 1
## 41 000000000010100 1 PopC: 3 HamD: 3
## 31 000000000001111 1 PopC: 5 HamD: 4
## 47 000000000010111 1 PopC: 5 HamD: 2
## 71 000000000100011 1 PopC: 4 HamD: 3
## 107 000000000110101 1 PopC: 5 HamD: 3
## 161 000000001010000 1 PopC: 3 HamD: 4
## 121 000000000111100 1 PopC: 5 HamD: 4
## 91 000000000101101 1 PopC: 5 HamD: 2
## 137 000000001000100 1 PopC: 3 HamD: 4
## 103 000000000110011 1 PopC: 5 HamD: 6
## 155 000000001001101 1 PopC: 5 HamD: 6
## 233 000000001110100 1 PopC: 5 HamD: 4
## 175 000000001010111 1 PopC: 6 HamD: 3
## 263 000000010000011 1 PopC: 4 HamD: 4
## 395 000000011000101 1 PopC: 5 HamD: 3
## 593 000000100101000 1 PopC: 4 HamD: 7
## 445 000000011011110 1 PopC: 7 HamD: 7
## 167 000000001010011 1 PopC: 5 HamD: 4
## 251 000000001111101 1 PopC: 7 HamD: 4
## 377 000000010111100 1 PopC: 6 HamD: 3
## 283 000000010001101 1 PopC: 5 HamD: 3
## 425 000000011010100 1 PopC: 5 HamD: 4
## 319 000000010011111 1 PopC: 7 HamD: 4
## 479 000000011101111 1 PopC: 8 HamD: 3
## 719 000000101100111 1 PopC: 7 HamD: 3
##1079 000001000011011 1 PopC: 6 HamD: 7
##1619 000001100101001 1 PopC: 6 HamD: 4
##2429 000010010111110 1 PopC: 8 HamD: 8
## 911 000000111000111 1 PopC: 7 HamD: 7
##1367 000001010101011 1 PopC: 7 HamD: 6
##2051 000010000000001 1 PopC: 3 HamD: 6
##3077 000011000000010 1 PopC: 4 HamD: 3
## 577 000000100100000 1 PopC: 3 HamD: 5
## 433 000000011011000 1 PopC: 5 HamD: 6
## 325 000000010100010 1 PopC: 4 HamD: 5
## 61 000000000011110 1 PopC: 5 HamD: 5
## 23 000000000001011 1 PopC: 4 HamD: 3
## 35 000000000010001 1 PopC: 3 HamD: 3
## 53 000000000011010 1 PopC: 4 HamD: 3
## 5 000000000000010 1 PopC: 2 HamD: 2
## 1 000000000000000 1 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]