简体   繁体   中英

Python - bitshifting with 32bit version vs 64bit versions

Context

I have an application which outputs data into files and which verifies the data via checksums. While everything was working correctly with Python 3.8.0 32bit , I observed different checksums being generated with Python 3.8.4 64bit . Following the data that was being dumped incorrectly, I believe I've narrowed it down to this code (as the files that fail are the only ones using this function).

The purpose of this method is to take a single value, 0-65535, and split it into two separate "bytes".

def integer_to_lsb_msb(value: int) -> List[int]:

    if value < 0 or value > 65535:
        raise ValueError("The input value does not qualify as a '16bit' value", value)

    lsb = value & 0xFF
    msb = ((value >> 8) & 0xFF)
    return [lsb, msb]

Question

Does python not process code in an architecture independent way? If not, is there a way to check the architecture and run custom code to ensure identical output with both 32bit and 64bit versions?

The python interpreter should run the code independent from the architecture. So you shouldn't worry about it in your code. For instance, "All integers are implemented as “long” integer objects of arbitrary size" ( Python Doc ).

Nevertheless you may encounter problems if you load external libraries whose bitness is different from your interpreter's ( Should I use Python 32bit or Python 64bit ), such as DLL's in Windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM