Python update trouble (2.3 to 2.4): x<<y

John Machin sjmachin at lexicon.net
Sun May 21 09:40:32 CEST 2006


For a start,
>>> 22002496167782427386022437441624938050682666541682 & 0xffffffffL
67560050L
>>>
so you could just do that at the end.
But you don't really want to be carrying all that rubbish around,
getting longer and longer each time around the loop. And let's further
the translation into Python by getting rid of the xrange gizmoid and a
few other undesirables:

# tested on your 1 test value
def CalcCRC16v2(astr): # don't shadow str()
    # global gCRC16Table # don't need this
    crc = 0xFFFFL # start as a long
    for c in astr:
        crc = gCRC16Table[((crc >> 8) & 255)] ^ ((crc & 0xFFFFFF) << 8)
^ ord(c)
    return crc

That should get you going. Although the whole calculation will be done
with longs instead of ints, the longs will never exceed 32 bits so it
shouldn't be too slow.

HTH,
John




More information about the Python-list mailing list