Skip to content

Instantly share code, notes, and snippets.

@beyondlimits
Created February 16, 2019 13:51
Show Gist options
  • Select an option

  • Save beyondlimits/9bebb798060dde6631871b76da4ce837 to your computer and use it in GitHub Desktop.

Select an option

Save beyondlimits/9bebb798060dde6631871b76da4ce837 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
# Computes bit parity. When x represented as binary number
# has odd number of ones, 1 is returned. Otherwise 0 is returned.
def parity(x):
i = 1
y = x
while x:
x >>= i
y ^= y >> i
i <<= 1
return y & 1
# bits - number of bits in register, used to know where to put incoming bit.
def next_word(previous_word, polynomial, bits):
return (previous_word >> 1) | (parity(previous_word & polynomial) << (bits-1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment