Bit Operations

Dan Upton upton at virginia.edu
Wed Nov 28 16:01:33 EST 2007


>  >>> 0xff & (((0xff & a) << 4) | (0xff & b))
> 150
>
> or, if you're sloppy,
>
>  >>> (a << 4) | b
> 150

Slightly OT, maybe - why exactly is the second alternative 'sloppy?'
I believe you, because I had a problem once (in Java) with bytes not
having the value I expected unless I did the and-magic, but I wasn't
clear on why.  Is it an issue with the word otherwise possibly not
being zeroed out?

-dan



More information about the Python-list mailing list