Zero-fill shift
Dan Bishop
danb_83 at yahoo.com
Tue May 4 17:57:33 EDT 2004
Daniel Orner <cs993442 at cs.yorku.ca> wrote in message news:<mailman.241.1083693910.25742.python-list at python.org>...
> Hey all,
>
> I'm trying to port a (relatively) simple encryption algorithm from Java
> code (mainly because the algorithm will be used identically in both
> contexts). However, the code makes extensive use of Java's >>> operator,
> which shifts right and fills in the leftmost bits with zeroes. I've been
> unable to duplicate that effect in Python.
> Apparently, a >>> b is equal to the following:
>
> if a >= 0: return a >> b
> else: return (a>>b)+(2<<~b)
>
> However, Python complains when I try to do the left-shift, because ~b
> is often a negative number. Does anyone have a better idea about how I
> should go about doing this?
def srl(a, b):
return (a & 0xFFFFFFFFL) >> b
"a & 0xFFFFFFFFL" behaves as if you had written "(unsigned int) a" in C.
More information about the Python-list
mailing list