Talk:Gray code: Difference between revisions

(nvm: was about decoding)
Line 9:
 
Decoding individual symbols looks trivial; you could do it with a lookup table. What would decoding a Gray code bitstream look like? Finding the the beginning of each symbol might be a tad trickier. --[[User:Short Circuit|Michael Mol]] 13:13, 18 March 2011 (UTC)
 
==Python bin<>int translations==
Someone has just modified the Python bin2int function to use intermediate 'string' functions. (The analogous dual of the int2bin function would be to parse and split the output of bin(n) then turn the split bits into a list of integers i.e: <code>[int(b) for b in bin(n)[2:]]</code>)
 
* I had thought of doing that myself and rejected it as I didn't want to introduce string types as well, so keeping the focus more on the gray code conversions.
 
* Using the 'string' versions does make the functions smaller however.
 
I did time the two versions of bin2int and the new version (bintint2 below) is 3 times slower so there is a cost.
<lang python>>>> Timer('for b in n: bin2int(b)', setup).repeat(1)
[6.267737098708267]
>>> setup = '''
def bin2int(bits):
'From binary bits, msb at index 0 to integer'
i = 0
for bit in bits:
i = i * 2 + bit
return i
 
def bin2int2(bits):
'From binary bits, msb at index 0 to integer'
return int(''.join(str(b) for b in bits), 2)
 
import random
n = [[1] + [random.choice((0,1)) for i in range(31)] for j in range(10)]
 
'''
>>> Timer('for b in n: bin2int(b)', setup).repeat(1)
[60.964307340517905]
>>> Timer('for b in n: bin2int2(b)', setup).repeat(1)
[188.2841723752312]</lang>
 
Personally I would prefer to use the none-string versions, but I can't decide if the string versions are easier to read/maintain. There is no speed penalty when considering the needs of the RC task. I do think that the original versions might be readable by a larger % of the people that are trying to understand Gray coding through the referenced animation though. --[[User:Paddy3118|Paddy3118]] 09:26, 23 March 2011 (UTC)
Anonymous user