Computer Network Tutorial: Error Detection (Parity, CRC, Checksum)

beginner
5 min

Computer Network Tutorial: Error Detection (Parity, CRC, Checksum)

Welcome to our deep dive into the fascinating world of Error Detection in computer networks! In this comprehensive guide, we'll explore three common methods used to ensure data integrity during transmission: Parity, CRC (Cyclic Redundancy Check), and Checksum. Let's get started!

Understanding Error Detection

Before we dive into the specific techniques, let's first understand why error detection is crucial. Data transmitted over a network can be susceptible to errors due to various reasons, such as noise, hardware failures, or software glitches. Error detection helps identify and correct these errors to ensure the received data matches the sent data.

Parity Check

šŸ’” Pro Tip: Parity is a simple method used to detect an odd or even number of 1s in a data frame.

Even Parity

In even parity, the number of 1s in a data frame plus the parity bit should always be even.

python
def even_parity(data): parity = 0 for bit in data: parity ^= bit return not parity data = [0, 1, 0, 1] parity_bit = 1 if even_parity(data) else 0 data.append(parity_bit) print(data) # [0, 1, 0, 1, 1]

Odd Parity

In odd parity, the number of 1s in a data frame plus the parity bit should always be odd.

python
def odd_parity(data): parity = 0 for bit in data: parity ^= bit return parity data = [0, 1, 0, 1] parity_bit = 0 if odd_parity(data) else 1 data.append(parity_bit) print(data) # [0, 1, 0, 1, 0]

šŸ“ Note: Parity is a simple method but doesn't provide much error-detection power as it can only detect a single error when a data frame is transmitted over multiple frames.

CRC (Cyclic Redundancy Check)

šŸ’” Pro Tip: CRC is a more robust error-detection method that uses a polynomial function to generate a checksum.

CRC checksum is calculated by dividing the message data by a specific polynomial. The remainder is the checksum appended to the message. During reception, the same calculation is performed, and if the checksums match, the data is considered error-free.

python
def crc16(data, polynomial=0x1021): crc = 0xFFFF for byte in data: for bit in bin(byte)[2:][::-1]: crc = (crc >> 1) ^ (0x8000 * (crc & 1) ^ polynomial if int(bit) == 1 else crc >> 1) return crc message = b'\x01\x02\x03\x04' checksum = crc16(message) message += checksum.to_bytes(2, byteorder='little') print(message.hex()) # 01020304a64b

šŸ“ Note: In this example, we've used the popular CRC-16 CCITT polynomial for demonstration purposes.

Checksum

šŸ’” Pro Tip: Checksum is a simple method that calculates the sum of all bytes in a data frame and reverses the result.

python
def checksum(data): total = sum(data) return (total & 0xFFFF) + (total >> 16) message = [0x01, 0x02, 0x03, 0x04] checksum_value = checksum(message) message.append(checksum_value) message.reverse() print(message) # [107, 18, 3, 4, 178]

šŸ“ Note: Checksum is a simple method but provides less error-detection power compared to CRC.

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of error detection in computer networks?


In this tutorial, we've explored three error-detection methods: Parity, CRC, and Checksum. Each method has its strengths and weaknesses, and the choice of which to use depends on the specific requirements of your application.

We hope this guide has helped you understand error detection techniques in computer networks. Happy coding! šŸŽÆ