Binary number manipulation

Tim Roberts timr at probo.com
Wed Dec 3 01:27:06 EST 2003


SBrunning at trisystems.co.uk wrote:
> 
>> 2. If 1 is no, does that mean that I need to do all the manipulation in
>> some icky string format and then go back?
> 
>I use:
>
>((high << 16) | low)

...which, unfortunately, gives a FutureWarning in Python 2.3 if high
happens to be larger than 32767.

I think we're all going to be using a lot more of the struct module in the
future.  For example, this is the equivalent of what you posted:

  struct.unpack('>L',struct.pack('>2H',high,low))[0]

I may have to start using things like:

def MAKELONG(high,low):
  return struct.unpack('>L',struct.pack('>2H',high,low))[0]

Is there a smarter way to do this kind of thing?  This also works:
  ((high << 16L) | low)
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.




More information about the Python-list mailing list