def crc(data):
    """Calculate 16-bit CRC-16-CCITT for data."""
    rem = 65535
    for d in bytearray(data):
        rem ^= d << 8
        for _ in range(8):
            rem <<= 1
            if rem & 65536:
                rem ^= 4129
            rem &= 65535
    return rem ^ 65535
