mirror of
https://github.com/lowobservable/coax.git
synced 2026-02-16 13:03:11 +00:00
27 lines
648 B
Python
27 lines
648 B
Python
"""
|
|
coax.parity
|
|
~~~~~~~~~~~
|
|
|
|
Single byte parity computation
|
|
"""
|
|
|
|
# From http://p-nand-q.com/python/algorithms/math/bit-parity.html
|
|
def _parallel_swar(i):
|
|
i = i - ((i >> 1) & 0x55555555)
|
|
i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
|
|
i = (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24
|
|
return int(i % 2)
|
|
|
|
_PARITY_LOOKUP = [_parallel_swar(i) for i in range(256)]
|
|
|
|
def even_parity(byte):
|
|
"""Compute even parity"""
|
|
if byte < 0 or byte > 255:
|
|
raise ValueError('Input must be between 0 and 255')
|
|
|
|
return _PARITY_LOOKUP[byte]
|
|
|
|
def odd_parity(byte):
|
|
"""Compute odd parity"""
|
|
return int(not even_parity(byte))
|