登录后复制 def parity_check(data): # 计算数据中 1 的个数 # 将16进制转10进制int then to 2进制str count = bin(int(data, 16))[2:].count('1') # 根据 1 的个数确定奇偶位 parity = '0' if count % 2 == 0 else '1' return parity # 示例数据 data = '10110101' # 进行奇偶校验 parity = parity_check(data) # 输出奇偶位 print("奇偶位:", parity) 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19. 登录后复制 def to_bin(x, length=0): binary_str = bin(x)[2:] return binary_str.zfill(length) def print_xor(a, b): max_length = max(len(to_bin(a)), len(to_bin(b))) print(to_bin(a, max_length), to_bin(b, max_length)) result = a ^ b print(to_bin(result, max_length)) print_xor(5, 3) 1.2.3.4.5.6.7.8.9.10.11.12.13. 原创作者: save 转载于: https://blog.51cto.com/save/11351813