gpt4 book ai didi

python - 如何使用 ctypes 获取 32 位 int 的前 11 位

转载 作者:行者123 更新时间:2023-11-28 22:53:21 25 4
gpt4 key购买 nike

如何使用 ctypes 获取 32 位 int 的前 11 位?

import ctypes

class Fields(ctypes.Structure):
_pack_ = 1
_fields_ = [('a', ctypes.c_uint, 11)]

class BitField(ctypes.Union):
_pack_ = 1
_fields_ = [('b', Fields),
('raw', ctypes.c_uint)]

bf = BitField()
bf.raw = 0b01010000001000000000000000000001

print('0b{:0>32b}'.format(bf.raw))
print('0b{:0>32b}'.format(bf.b.a))

结果:

0b01010000001000000000000000000001
0b00000000000000000000000000000001

虽然我想要

0b010100000010000000000000000000010b00000000000000000000001010000001

最佳答案

另一种选择是使用

class Fields(ctypes.Structure):
_pack_ = 1
_fields_ = [('x', ctypes.c_uint, 21), ('a', ctypes.c_uint, 11)]

关于python - 如何使用 ctypes 获取 32 位 int 的前 11 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19541493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com