Newbie: unsigned shift right
dickinsm at gmail.com
Wed Mar 26 23:02:46 CET 2008
On Mar 26, 5:42 pm, Sal <h... at softcom.net> wrote:
> Is there any way to do an unsigned shift right in Python? When I enter
> (-1>>1) the answer is -1. What I'm looking for is the equivalent of an
> unsigned shift in C or the ">>>" operator in Java.
What answer were you hoping for, and why? 2**31-1? 2**63-1?
If you're thinking of the -1 as representing a particular
fixed-width bit pattern, try doing a bitwise 'and' operation
with a suitable mask first. For example, if you're thinking
of -1 as a 32-bit quantity, then
(-1 & (2**32-1)) >> 1
might produce what you want.
More information about the Python-list