def crc16(string, value=0):
 crc16_table = []
 for byte in range(256):
  crc = 0
  for bit in range(8):
   if ((byte ^ crc) & 1):
    crc = ((crc >> 1) ^ 40961)
   else:
    crc >>= 1
   byte >>= 1
  crc16_table.append(crc)
 for ch in string:
  value = (crc16_table[(ord(ch) ^ (value & 255))] ^ (value >> 8))
 return value

def crc16 ( string , value = 0 ) : crc16_table = [ ] for byte in range ( 256 ) : crc = 0 for bit in range ( 8 ) : if ( ( byte ^ crc ) & 1 ) : crc = ( ( crc >> 1 ) ^ 40961 ) else : crc >>= 1 byte >>= 1 crc16_table . append ( crc ) for ch in string : value = ( crc16_table [ ( ord ( ch ) ^ ( value & 255 ) ) ] ^ ( value >> 8 ) ) return value
crc-16 poly: p(x) = x**16 + x**15 + x**2 + 1 .