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

Next revision
Previous revision
tanszek:oktatas:techcomm:parity_check [2024/10/06 17:47] – created kneheztanszek:oktatas:techcomm:parity_check [2024/11/04 19:00] (current) knehez
Line 29: Line 29:
   * If the number of **1s** is odd, an error is detected.   * If the number of **1s** is odd, an error is detected.
  
 +==== Use of Parity Check ====
  
 +Parity checks are commonly used in:
 +
 +  * **Data transmission protocols**: To ensure data integrity when sending bits over a communication channel.
 +  * **Memory systems**: Error-detecting memory like Parity RAM uses parity bits to detect errors in stored data.
 +  * **Magnetic storage and hard drives**: To ensure that stored data hasn't been corrupted.
 +
 +Disadvantages:
 +
 +  * **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.
 +
 +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.1728236836.txt.gz · Last modified: 2024/10/06 17:47 by knehez