tanszek:oktatas:techcomm:parity_check
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tanszek:oktatas:techcomm:parity_check [2024/10/06 17:49] – [Example] knehez | tanszek:oktatas:techcomm:parity_check [2024/11/04 19:00] (current) – knehez | ||
---|---|---|---|
Line 41: | Line 41: | ||
* **Limited error detection**: | * **Limited error detection**: | ||
* **No error correction**: | * **No error correction**: | ||
+ | |||
+ | Let's see a C code to check 8 bits parity. | ||
+ | |||
+ | <sxh c> | ||
+ | #include < | ||
+ | |||
+ | int check_parity(unsigned char num) { | ||
+ | int count = 0; | ||
+ | |||
+ | // Count the number of 1 bits | ||
+ | while (num) { | ||
+ | count += num & 1; | ||
+ | num >>= 1; | ||
+ | } | ||
+ | |||
+ | // If the count of 1 bits is even, the parity is correct | ||
+ | return (count % 2 == 0); | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | char binary[9]; // 8 bits + 1 space for the null terminator | ||
+ | unsigned char num = 0; | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | // Convert the binary string to an 8-bit unsigned integer | ||
+ | for (int i = 0; i < 8; i++) { | ||
+ | num <<= 1; // Shift bits to the left by one position | ||
+ | if (binary[i] == ' | ||
+ | num |= 1; // Set the lowest bit if the current character is ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (check_parity(num)) { | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |
tanszek/oktatas/techcomm/parity_check.1728236973.txt.gz · Last modified: 2024/10/06 17:49 by knehez