C Language Example_Parity Check Algorithm

1. Parity check algorithm

Parity Check Algorithm (Parity Check Algorithm) is a simple error detection method used to verify whether a bit error has occurred during data transmission. Error detection and correction is achieved by adding an additional parity bit (check bit) to the data.

In the parity algorithm, it is assumed that each byte consists of 8 bits (bits). The value of the parity bit depends on the number of 1s in the data byte. If the number of 1s in the data byte is even, the parity bit is set to 0; if the number of 1s is odd, the parity bit is set to 1. In this way, at the receiving end, bit errors can be detected by counting the number of 1s in the received data bytes.

The specific parity check algorithm includes the following steps:

(1) Sending end: Before sending the data byte, count the number of 1s in the data byte, set the value of the parity bit according to the number, and send the data byte and the parity bit together.

(2) Receiver: After receiving the data byte, count the number of 1s in the received data byte again, and compare it with the received parity bit. If the two do not match, a bit error has occurred in the data transmission.

The parity check algorithm is often used in the following scenarios:

(1) Serial communication: In serial communication, the parity check algorithm can be used to detect bit errors that occur during data transmission. The sending end calculates the parity bit and appends it to the sent data bytes, and the receiving end judges whether the received data is correct by verifying the parity bit.

(2) Storage media: On some storage media, such as disk drives or flash memory, parity-check algorithms can be used to detect bit errors that occur during data reading or writing. When storing data, calculate the parity bit and store it together with the data; when reading data, calculate the check bit again and compare it with the stored check bit to ensure the integrity and accuracy of the data.

(3) Error detection: The parity-check algorithm can also be used in other scenarios that require simple error detection. For example, in computer memory or registers, parity bits can be used to detect bit errors in the process of storing data to avoid misuse or transmission of data.

A parity-checking algorithm can only detect bit errors, not correct them. If an error is detected, additional error-correcting action or a request to retransmit the data is required.

image-20230626214339103

image-20230626214412782

2. Code implementation

Scenario: In single-chip communication, the single-chip needs to send data to the host computer. The following code demonstrates two functions, which are used by the sender and the receiver, and use the parity check algorithm to verify the data.

2.1 Sender function

void sender_send_data_with_parity(unsigned char* data, int length) {
    
    
    // 统计数据字节中1的个数
    int count = 0;
    for (int i = 0; i < length; i++) {
    
    
        unsigned char byte = data[i];
        for (int j = 0; j < 8; j++) {
    
    
            if ((byte >> j) & 1) {
    
    
                count++;
            }
        }
    }

    // 计算奇偶校验位,如果1的个数是偶数,则校验位为0,否则为1
    unsigned char parity_bit = (count % 2 == 0) ? 0 : 1;

    // 发送数据字节和奇偶校验位
    for (int i = 0; i < length; i++) {
    
    
        send_byte(data[i]);
    }
    send_byte(parity_bit);
}

2.2 Receiver function

void receiver_receive_data_with_parity() {
    
    
    // 接收数据
    unsigned char received_data[MAX_LENGTH];
    int length = receive_data(received_data);

    // 统计接收到的数据字节中1的个数
    int count = 0;
    for (int i = 0; i < length - 1; i++) {
    
    
        unsigned char byte = received_data[i];
        for (int j = 0; j < 8; j++) {
    
    
            if ((byte >> j) & 1) {
    
    
                count++;
            }
        }
    }

    // 比较接收到的奇偶校验位与数据字节中1的个数是否一致
    unsigned char expected_parity_bit = (count % 2 == 0) ? 0 : 1;
    unsigned char received_parity_bit = received_data[length - 1];

    if (expected_parity_bit != received_parity_bit) {
    
    
        // 发生了位错误
        handle_error();
    } else {
    
    
        // 数据传输正常
        process_data(received_data, length - 1);
    }
}

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/132269345