How do I truncate my return value to 32-bits? I have the following code:
def rotate_left(value, shift):
return hex((value << shift) | (value >> (32 - shift)))
I would like the return value to be
0x0000_000A
instead of 0xA_0000_000A
when calling rotate_right(0xA00_0000)
0xFFFFFFFF
是 32 位,所以你可以做一个逻辑或:
result = number & 0xFFFFFFFF
If you'd prefer to do this generally and not just for 32 bits:
def truncate(value, bit_count):
return value & (2**bit_count - 1)
This works because 2**bit_count -1
is all 1's in binary.
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.