Skip to content

Instantly share code, notes, and snippets.

@hamzamuric
Created February 11, 2019 20:22
Show Gist options
  • Save hamzamuric/29a43b1765c3e9b6d4b4a2c3dbbff27e to your computer and use it in GitHub Desktop.
Save hamzamuric/29a43b1765c3e9b6d4b4a2c3dbbff27e to your computer and use it in GitHub Desktop.
CRC python implementation
import math
def leftmostone(x):
return math.floor(math.log2(x))
def podeli(a, b):
return int(leftmostone(a) == leftmostone(b))
a = int(input('Unesi broj: '))
b = int(input('Unesi delitelj: '))
a <<= leftmostone(b)
shift_count = 0
i = 0
while leftmostone(a >> i) != leftmostone(b):
shift_count += 1
i += 1
deljenik = a >> shift_count
i = 1 << shift_count - 1
while i > 0:
q = podeli(deljenik, b)
spust = q * b
deljenik ^= spust
deljenik <<= 1
deljenik |= int((a & i) != 0)
i >>= 1
print("CRC: {}".format(deljenik))
print("DATA|CRC = {}".format(a | deljenik))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment