User Tools

Site Tools


tanszek:oktatas:techcomm:parity_check

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tanszek:oktatas:techcomm:parity_check [2024/10/06 17:49] – [Example] kneheztanszek:oktatas:techcomm:parity_check [2024/11/04 19:00] (current) knehez
Line 41: Line 41:
   * **Limited error detection**: Parity checks can only detect single-bit errors (if one bit flips). It cannot detect multi-bit errors, where two or more bits are changed.   * **Limited error detection**: Parity checks can only detect single-bit errors (if one bit flips). It cannot detect multi-bit errors, where two or more bits are changed.
   * **No error correction**: Parity checks only detect errors but cannot correct them. For correction, more advanced techniques like Hamming codes.   * **No error correction**: Parity checks only detect errors but cannot correct them. For correction, more advanced techniques like Hamming codes.
 +
 +Let's see a C code to check 8 bits parity.
 +
 +<sxh c>
 +#include <stdio.h>
 +
 +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("Enter an 8-bit binary number: ");
 +    scanf("%8s", binary);
 +
 +    // 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] == '1') {
 +            num |= 1;             // Set the lowest bit if the current character is '1'
 +        }
 +    }
 +
 +    if (check_parity(num)) {
 +        printf("Parity is correct (even number of 1 bits).\n");
 +    } else {
 +        printf("Parity is incorrect (odd number of 1 bits).\n");
 +    }
 +
 +    return 0;
 +}
 +</sxh>
tanszek/oktatas/techcomm/parity_check.1728236973.txt.gz · Last modified: 2024/10/06 17:49 by knehez