Created
February 11, 2019 20:22
-
-
Save hamzamuric/29a43b1765c3e9b6d4b4a2c3dbbff27e to your computer and use it in GitHub Desktop.
CRC python implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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