1. Algorithm for Single Byte Parity Check Code
Principle of Parity Check Code
The parity check code is used to detect errors in data transmission. There are two types:
1.Even Parity: Ensures that the number of1s in the data is even.
2.Odd Parity: Ensures that the number of1s in the data is odd.
Here, we take the byte (8 bits) parity check as an example to calculate the parity bit for each byte.
Steps for Code Implementation
1. Include Header Files
#include
2. Parity Check Function
// The XOR operation is a widely used binary operation in mathematics and computer science. It is characterized by the fact that if the bits of the two operands are the same, the result is 0; if different, the result is 1. // Calculate the odd parity bit for a single byte (returns 1 if the number of 1s is odd, otherwise returns 0) uint8_t parity_bit_odd(uint8_t byte) { uint8_t parity = 0; for (int i = 0; i < 8; i++) { parity ^= (byte >> i) & 0x01;// Count the number of 1s using bitwise XOR } return parity;// Return the odd parity bit (0 or 1) } // Calculate the parity code for the data (generate parity bits byte by byte and store them in an array) void generate_parity_bits(const uint8_t *data, size_t len, uint8_t *parity) { for (size_t i = 0; i < len; i++) { parity[i] = parity_bit_odd(data[i]); } }
3. Test Function
// Test the odd parity bit for a single byte void test_parity_bit_odd() { // Test cases: input byte -> expected parity bit assert(parity_bit_odd(0x00) == 0);// 0x00 (00000000) has 0 ones, should return 1 for odd parity? assert(parity_bit_odd(0x01) == 1);// 0x01 (00000001) has 1 one, should return 0 for odd parity assert(parity_bit_odd(0xFF) == 0);// 0xFF (11111111) has 8 ones, should return 0 for odd parity assert(parity_bit_odd(0xAA) == 0);// 0xAA (10101010) has 4 ones, should return 1 for odd parity? } // Test the generation of parity bits for a data block void test_generate_parity_bits() { uint8_t data[] = {0x00, 0x01, 0xFF, 0xAA}; uint8_t parity[4]; uint8_t expected[] = {0, 1, 0, 0};// Expected parity bits based on odd parity rules generate_parity_bits(data, 4, parity); for (int i = 0; i < 4; i++) { assert(parity[i] == expected[i]); } }
4. Main Function Call Test
int main() { test_parity_bit_odd(); test_generate_parity_bits(); printf(“All parity tests passed!\n”); return 0; }
Code Explanation
1.Function parity_bit_odd:
oInput a byte (uint8_t), count the number of1s in the binary representation using bitwise XOR.
oIf the number of1s is odd, return0 (no padding needed); if even, return1 (padding to make the total count odd).
2.Function generate_parity_bits:
oInput a data array and length, generate odd parity bits for each byte, and store them in the output arrayparity.
3.Test Cases:
otest_parity_bit_odd: Test the calculation of the parity bit for a single byte.
otest_generate_parity_bits: Test the generation of parity codes for a data block.
Test Results
·If all assertions pass, output All parity tests passed!.
·If any test fails, the program will terminate and indicate the error location.
Boundary Case Testing
// Add more tests to test_parity_bit_odd void test_parity_bit_odd() { // …original tests… assert(parity_bit_odd(0x80) == 1); // 0x80 (10000000) has 1 one, should return 0 for odd parity assert(parity_bit_odd(0x7F) == 1); // 0x7F (01111111) has 7 ones, should return 0 for odd parity }
Complete Test Code
#include
2. Algorithm for Multi-Byte Parity Check Code
The multi-byte bitwise XOR parity check code generates a checksum byte by performing XOR operations on each bit of multiple bytes to detect the integrity of data blocks.
1. Include Header Files
#include
2. Bitwise XOR Check Function
/** * @brief Calculate the bitwise XOR checksum for multi-byte data * @param data Input data array * @param len Data length (in bytes) * @return Checksum (1 byte) */ uint8_t parity_xor_multibyte(const uint8_t *data, size_t len) { uint8_t checksum = 0; for (size_t i = 0; i < len; i++) { checksum ^= data[i]; // XOR byte by byte } return checksum; }
3. Test Cases
// Test the multi-byte XOR checksum void test_parity_xor_multibyte() { // Test case 1: single byte input uint8_t data1[] = {0xAA}; uint8_t result = parity_xor_multibyte(data1, 1); assert(result == 0xAA); // Test case 2: multi-byte XOR result is 0x00 uint8_t data2[] = {0x01, 0x02, 0x03}; result = parity_xor_multibyte(data2, sizeof(data2)/sizeof(data2[0])); assert(result == 0x00); // Test case 3: multi-byte XOR result is 0xFF uint8_t data3[] = {0xAA, 0x55};// 0xAA: 10101010, 0x55: 01010101 result = parity_xor_multibyte(data3, 2); assert(result == 0xFF); // XOR result: 11111111 // Test case 4: includes overflow XOR operation uint8_t data4[] = {0xFF, 0x01};// 0xFF: 11111111, 0x01: 00000001 result = parity_xor_multibyte(data4, 2); assert(result == 0xFE); // XOR result: 11111110 }
4. Main Function Call Test
int main() { test_parity_xor_multibyte(); printf(“All multibyte XOR parity tests passed!\n”); return 0; }
5. Complete Code
#include
Code Explanation
1.Core Function parity_xor_multibyte:
oInput a byte array data and length len, perform XOR operation byte by byte.
oThe final returned checksum is the result of XORing all input bytes at each bit position.
2.Test Logic:
oTest Case 1: When input is a single byte, the checksum equals the input byte itself.
oTest Case 2: The XOR result of three bytes is 0x00 (0x01 ^ 0x02 ^ 0x03 = 0x00).
oTest Case 3:0xAA and 0x55 XOR result is 0xFF (complement at corresponding bits).
oTest Case 4: Verify the XOR result including all 1s and a single 1.
Test Results
·If all assertions pass, the program outputs:All multibyte XOR parity tests passed!.
·If any test fails, the program will terminate and indicate the error location.
Application Scenarios
·Communication Protocols: Detecting data transmission integrity in protocols like UART, I2C, etc.
·Storage Verification: Verifying consistency of written and read data blocks.
·Simple Error Correction: Can detect single-bit errors when combined with other algorithms.
Advantages and Disadvantages of the Algorithm
Advantages |
Disadvantages |
Simple implementation, fast computation |
Can only detect odd number of bit errors |
Low resource usage (suitable for embedded systems) |
Cannot correct errors |
Suitable for quick checks of short data blocks |
Higher probability of conflicts with long data blocks |