How to get the signed integer value of a long in python?

后端 未结 6 1789
攒了一身酷
攒了一身酷 2020-12-08 15:40

If lv stores a long value, and the machine is 32 bits, the following code:

iv = int(lv & 0xffffffff)

results an iv of type long, inst

6条回答
  •  眼角桃花
    2020-12-08 15:51

    You're working in a high-level scripting language; by nature, the native data types of the system you're running on aren't visible. You can't cast to a native signed int with code like this.

    If you know that you want the value converted to a 32-bit signed integer--regardless of the platform--you can just do the conversion with the simple math:

    iv = 0xDEADBEEF
    if(iv & 0x80000000):
        iv = -0x100000000 + iv
    

提交回复
热议问题