Advertisement
prabhavms

CRC

Nov 19th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | Source Code | 0 0
  1. def crc(data, divisor, mode):
  2.     n = len(divisor)
  3.     dividend = data + ('0' * (n - 1)) if mode == 'sender' else data
  4.     remainder = int(dividend[:n], 2)
  5.     for bit in dividend[n:]:
  6.         remainder = (remainder << 1) | int(bit)
  7.         if remainder & (1 << (n - 1)):
  8.             remainder ^= int(divisor, 2)
  9.     remainder = remainder & ((1 << (n - 1)) - 1)
  10.     return remainder
  11.  
  12. data = input("Data word (K): ")
  13. divisor = input("Divisor (N): ")
  14. remainder = crc(data, divisor, 'sender')
  15. codeword = data + f'{remainder:0{len(divisor)-1}b}'
  16. print("\nSender Side:\nCodeword (K+N):", codeword)
  17.  
  18. received = input("\nReceiver Side:\nEnter received Codeword (K+N): ")
  19. remainder = crc(received, divisor, 'receiver')
  20. print("Remainder:", remainder, "\nStatus:", "Accepted" if remainder == 0 else "Discarded")
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement