[Tutor] Fwd: how to see a number as two bytes

Steve Willoughby steve at alchemy.com
Mon Oct 20 22:51:22 CEST 2008


> > high, low = ((num % 2**16) >> 8, num % 2**8)  or something thereabouts.

My take would be something like

high, low = (num >> 8) & 0xff , num & 0xff

In case you want another option.  This is probably more
efficient since you're not raising to powers or taking the
modulus, although for all I know Python may optimize that
in these special cases anyway.  

Also, unless Python is doing more than I think it does
to watch out for your safety behind the scenes, this
is more safe against sign extension errors.

-- 
Steve Willoughby    |  Using billion-dollar satellites
steve at alchemy.com   |  to hunt for Tupperware.


More information about the Tutor mailing list