Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def ones_complement(value, bits):
- mask = (1 << bits) - 1
- return (~value) & mask
- def binary_sum(frames, bits):
- total = sum(int(frame, 2) for frame in frames)
- while total >> bits: # Handle carry
- total = (total & ((1 << bits) - 1)) + (total >> bits)
- return total
- def sender(frames, bits):
- total = binary_sum(frames, bits)
- checksum = ones_complement(total, bits)
- data_to_send = frames + [f'{checksum:0{bits}b}']
- print("\nSender Side:")
- print("Total Sum (Binary):", f'{total:0{bits}b}')
- print("Checksum (1's complement):", f'{checksum:0{bits}b}')
- print("Data to be sent:", ' '.join(data_to_send))
- return data_to_send
- def receiver(received_frames, bits):
- total = binary_sum(received_frames, bits)
- calculated_checksum = ones_complement(total, bits)
- print("\nReceiver Side:")
- print("Received Frames:", ' '.join(received_frames[:-1]))
- print("Received Checksum:", received_frames[-1])
- print("Total Sum (Binary):", f'{total:0{bits}b}')
- print("Calculated Checksum (After Adding Received Checksum):", f'{calculated_checksum:0{bits}b}')
- print("Status:", "Approved (No Error)" if calculated_checksum == 0 else "Discarded (Error)")
- # Input
- K = int(input("Enter the number of frames to transmit (K): "))
- N = int(input("Enter the number of bits in each frame (N): "))
- frames = [input(f"Enter frame {i+1} ({N} bits): ") for i in range(K)]
- # Sender Side
- data_to_send = sender(frames, N)
- # Receiver Side
- receiver(data_to_send, N)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement