ctypes位域问题

时间:2015-11-05 10:01:33

标签: python ctypes bit-fields

在这个例子中,我得到了ctypes的奇怪行为,其中每个c_bool字段似乎都大于1位。

import ctypes

class Bitfield(ctypes.Structure):
    _fields_ = [
            ("a", ctypes.c_uint16, 16),
            ("b", ctypes.c_bool, 1),
            ("c", ctypes.c_bool, 1),
            ("d", ctypes.c_bool, 1),
            ("e", ctypes.c_bool, 1),
            ("f", ctypes.c_bool, 1),
            ("g", ctypes.c_bool, 1),
            ("h", ctypes.c_bool, 1),
            ("i", ctypes.c_bool, 1),
            ("j", ctypes.c_bool, 1),
            ("k", ctypes.c_bool, 1),
            ("l", ctypes.c_bool, 1),
            ("m", ctypes.c_bool, 1),
            ("n", ctypes.c_bool, 1),
            ("o", ctypes.c_bool, 1),
            ("p", ctypes.c_bool, 1),
        ]

    def getdict(self):
        dict((f, getattr(self.bit, f)) for f, _, _ in self.bit._fields_)

class Status(ctypes.Union):
    _fields_ = [
            ("all",              ctypes.c_long),
            ("bit",              Bitfield),            
        ]

a = Status(0x20281234)
print hex(a.all) # Will display 0x20281234

a.bit.b = True   
print hex(a.all) # Should display 0x20291234, but display 0x20011234

a = Status(0x0)
print hex(a.all) # Will display 0x00000000

a.bit.b = True   
print hex(a.all) # Should display 0x0010000, and display 0x0010000

我的错误在哪里?

0 个答案:

没有答案
相关问题