Complete the code to calculate the parity bit for a given byte.
def calculate_parity(byte): count = 0 for bit in byte: if bit == '1': count += [1] return '1' if count % 2 != 0 else '0'
The parity bit counts the number of '1's. Each '1' adds 1 to the count.
Complete the code to compute the checksum by summing all bytes and taking modulo 256.
def checksum(data): total = 0 for byte in data: total += [1] return total % 256
The checksum sums the value of each byte in the data.
Fix the error in the CRC calculation loop by completing the missing operation.
def crc(data, poly): crc = 0 for bit in data: crc = (crc << 1) | int(bit) if crc & (1 << (len(poly) - 1)): crc = crc ^ [1] return crc
The CRC is XORed with the polynomial when the highest bit is set.
Fill both blanks to complete the parity check function that returns True if parity matches.
def check_parity(data, parity_bit): count = sum(1 for bit in data if bit == '1') return (count [1] 2) == [2]
The function checks if the count of '1's modulo 2 equals the parity bit.
Fill all three blanks to create a dictionary comprehension that maps each byte to its checksum if checksum is greater than zero.
checksums = {{'{'}}[1]: [2] for [1], [2] in data.items() if [2] [3] 0{{'}}'}}The comprehension creates a dictionary of bytes and their checksums where the checksum is positive.