当前位置: 首页 > 后端技术 > Python

python位域操作对齐问题

时间:2023-03-26 00:22:36 Python

定义了某个位域占多少位;但是python为了字节对齐还是会在中间插入很多0;后来加入了_pack_=1,在某些情况下还是有用的0插入;_pack_=1感觉有bug。使用ctypes时,位域必须8bit对齐。只有手动反汇编原来的位域才能解决这个问题。类mac_table_s(结构):_pack_=1_fields_=[(“outer_ethtype_sel”,c_uint8,3),#b02:0(“outer_pcp_data”,c_uint8,3),#b05:3(“outer_pcp_operation”,c_uint8,2)#b07:6]classmac_table_u(Union):_fields_=[("fields",mac_table_s),("packed",c_uint32*MAC_TABLE_SIZE_32BITWORDS)]unionsharedmemoryfields和packedsharedmemory,可以定义每个位域在fields,然后用packed以32bit为单位写入regenter