Skip to content

Instantly share code, notes, and snippets.

@veb-101
Last active November 18, 2019 20:44
Show Gist options
  • Save veb-101/073d712003b3ac0cc0ad44c07e0976e3 to your computer and use it in GitHub Desktop.
Save veb-101/073d712003b3ac0cc0ad44c07e0976e3 to your computer and use it in GitHub Desktop.
def crc(msg, div, code="000"):
msg += code
msg = list(msg)
div = list(div)
for i in range(len(msg) - len(code)):
if msg[i] == "1":
for j in range(len(div)):
msg[i+j] = f"{int(msg[i+j]) ^ int(div[j])}"
# msg[i+j] = f"{(int(msg[i+j])+int(div[j])) % 2}"
return ''.join(msg[-len(code):])
# suppose msg = ["1", "2", "3", "4"]
# and length(div) = 2
# o/p of ''.join(msg[-len(code):]) -> "34"
div = "1011"
msg = "10111011101011"
remainder0 = crc(msg, div)
print(remainder0)
# introducing error on any bit of msg
# to inverse the bit do xor
msg = list(msg) # convert string to list for chaning bit
msg[6] = str(int(msg[6]) ^ 1) # int(msg[6], 2) will convert string at index 6 in binary form
msg = "".join(msg) # convert list to string
remainder1 = crc(msg, div, remainder0)
print(remainder1)
# at sender side: remainder0 there will be some remainder
# add the msg and the remainder
# at reciver side if no noise occurs in communication remainder1 should be 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment