Need elegant way to cast four bytes into a long

Dan Bishop danb_83 at yahoo.com
Sun Aug 10 00:49:07 CEST 2003


Skip Montanaro <skip at pobox.com> wrote in message news:<mailman.1060448895.18308.python-list at python.org>...
> >> You mean like this:
>     >> 
>     >> l = long(a[0] + a[1] << 8 + a[2] << 16 + a[3] << 24)
>     >> 
>     John> Bzzzzzt. Oh the joys of operator precedence! 
> 
> Yeah, I realized that after seeing a couple other responses.  Should have
> kept my mouth shut.  I tend to think of << and >> as X2 and /2 operators and
> thus mentally lump them together with * / and %.  Fortunately, I don't do
> much bit twiddling or I'd be in real trouble...

One correct way of writing the expression without parentheses is 

a[0] | a[1] << 8 | a[2] << 16 | a[3] << 24




More information about the Python-list mailing list