Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def crc(data, divisor, mode):
- n = len(divisor)
- dividend = data + ('0' * (n - 1)) if mode == 'sender' else data
- remainder = int(dividend[:n], 2)
- for bit in dividend[n:]:
- remainder = (remainder << 1) | int(bit)
- if remainder & (1 << (n - 1)):
- remainder ^= int(divisor, 2)
- remainder = remainder & ((1 << (n - 1)) - 1)
- return remainder
- data = input("Data word (K): ")
- divisor = input("Divisor (N): ")
- remainder = crc(data, divisor, 'sender')
- codeword = data + f'{remainder:0{len(divisor)-1}b}'
- print("\nSender Side:\nCodeword (K+N):", codeword)
- received = input("\nReceiver Side:\nEnter received Codeword (K+N): ")
- remainder = crc(received, divisor, 'receiver')
- print("Remainder:", remainder, "\nStatus:", "Accepted" if remainder == 0 else "Discarded")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement