Hamming code check parity

randomname

I am not sure if I am calculating the parity bit correctly for the the check Parity bit function I wrote. The codeWord is 11 chars long with 4 parity bits and 7 data bits. Does the implementation look good?

void parityCheck(char* codeWord) {
int parity[4] = {0}, i = 0, diffParity[4] = {0}, twoPower = 0, bitSum = 0;

// Stores # of 1's for each parity bit in array.
parity[0] = (codeWord[2] - 48) + (codeWord[4] - 48) + (codeWord[6] - 48) + (codeWord[8] - 48) + (codeWord[10] - 48);
parity[1] = (codeWord[2] - 48) + (codeWord[5] - 48) + (codeWord[6] - 48) + (codeWord[9] - 48) + (codeWord[10] - 48);
parity[2] = (codeWord[4] - 48) + (codeWord[5] - 48) + (codeWord[6] - 48);
parity[3] = (codeWord[8] - 48) + (codeWord[9] - 48) + (codeWord[10] - 48);

// Determines if sum of bits is even or odd, then tests for difference from actual parity bit.
for (i = 0; i < 4; i++) {
    twoPower = (int)pow((double)2, i);

    if (parity[i] % 2 == 0)
            parity[i] = 0;
        else
            parity[i] = 1;

        if ((codeWord[twoPower-1] - 48) != parity[i])
            diffParity[i] = 1;
}

// Calculates the location of the error bit.
for (i = 0; i < 4; i++) {
    twoPower = (int)pow((double)2, i);
    bitSum += diffParity[i]*twoPower;
}



// Inverts bit at location of error.
if (bitSum <= 11 && bitSum > 0) {
    if ((codeWord[bitSum-1] - 48)) 
        codeWord[bitSum-1] = '0';
    else
        codeWord[bitSum-1] = '1';
}
MvG

Does the implementation look good?

This very much depends on your measure for “good”. I can confirm that it does get the job done, so at least it is correct. Your code is very verbose, and thus hard to check for correctness. I'd do the following:

int parity_check(int codeWord) {
  int parity = 0, codeWordBit, bitPos;
  for (bitPos = 1; bitPos <= 11; ++bitPos) {
    codeWordBit = ((codeWord >> (bitPos - 1)) & 1);
    parity ^= bitPos*codeWordBit;
  }
  if (parity != 0) {
    if (parity > 11)
      return -1; // multi-bit error!
    codeWord ^= 1 << (parity - 1);
  }
  return codeWord;
}

Instead of a sequence of digit characters, I treat your whole code word as a single integer, which is a lot more efficient.

Looking at the table at Wikipedia, I see that the columns of that table form binary representations of the sequence 1 … 11. Each code word bit affects exactly those parity bits mentioned in that column, so I take the code word bit (which is zero or one), multiply it by the bit pattern of that column to obtain either that pattern or zero, then XOR this with the current parity bit pattern. The effect of this is that a zero code word bit won't change anything, whereas a non-zero code word bit flips all associated parity bits.

Some care has to be taken because the bit pattern is one-based, whereas the bit position using the right shift trick is zero-based. So I have to subtract one, then shift right by that amount, and then extract the least significant digit in order to obtain the codeWordBit.

Using my implementation for reference, I was able to verify (by complete enumeration) that your code works the same.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Python XOR on hex and string (parity check function)

分類Dev

Find the parity outlier Javascript

分類Dev

generating numbers, with high hamming distance

分類Dev

Check the code coverage for a project on sonar with rest api

分類Dev

What is the code for a T-SQL check constraint?

分類Dev

Paypal Smart Button Check Postal Code onApprove

分類Dev

Check if latex code is processed by Pandoc or *tex

分類Dev

calculate pairwise Hamming distance and retain smaller one

分類Dev

Matlab Hamming Window to Vhdl 8-bit

分類Dev

Even/Odd parity for Even/Odd values

分類Dev

RAID 5 parity bits - recovering data

分類Dev

Is there a command line tool in Golang to only check syntax of my source code?

分類Dev

How can I check if a scroll was done by code or by user?

分類Dev

How to check exit code in bash in one line/statement?

分類Dev

Check if WooCommerce custom order status exist to execute code

分類Dev

What does "check out code" mean in git documentation for line endings?

分類Dev

A code to wait for certain number of minutes to check if condition is true

分類Dev

how to check how many lines of code in file using node js

分類Dev

Add a name to an array, check array to see if name exists, run code

分類Dev

Oracle: show min and max consecutive code but adding check digit

分類Dev

Can i check if a session is active throught the code instead at page load?

分類Dev

I need a make code that make a check that is ExtJS Checkbox checked or not

分類Dev

iOS8 - UILocalNotification, Check for permission later from code

分類Dev

Condensing code: Check that multiple columns follow a boolean in data frame in R

分類Dev

Can't get javascript working (beginner) - please check my code

分類Dev

Javascript code to check URL content and keep only one thing

分類Dev

I am trying to write a code but facing a java.lang.StringIndexOutOfBoundsException. Please check the code out

分類Dev

How to cleanly check HTTP return code is in server side exception code family

分類Dev

/ usr / bin / parityとは何ですか?

Related 関連記事

  1. 1

    Python XOR on hex and string (parity check function)

  2. 2

    Find the parity outlier Javascript

  3. 3

    generating numbers, with high hamming distance

  4. 4

    Check the code coverage for a project on sonar with rest api

  5. 5

    What is the code for a T-SQL check constraint?

  6. 6

    Paypal Smart Button Check Postal Code onApprove

  7. 7

    Check if latex code is processed by Pandoc or *tex

  8. 8

    calculate pairwise Hamming distance and retain smaller one

  9. 9

    Matlab Hamming Window to Vhdl 8-bit

  10. 10

    Even/Odd parity for Even/Odd values

  11. 11

    RAID 5 parity bits - recovering data

  12. 12

    Is there a command line tool in Golang to only check syntax of my source code?

  13. 13

    How can I check if a scroll was done by code or by user?

  14. 14

    How to check exit code in bash in one line/statement?

  15. 15

    Check if WooCommerce custom order status exist to execute code

  16. 16

    What does "check out code" mean in git documentation for line endings?

  17. 17

    A code to wait for certain number of minutes to check if condition is true

  18. 18

    how to check how many lines of code in file using node js

  19. 19

    Add a name to an array, check array to see if name exists, run code

  20. 20

    Oracle: show min and max consecutive code but adding check digit

  21. 21

    Can i check if a session is active throught the code instead at page load?

  22. 22

    I need a make code that make a check that is ExtJS Checkbox checked or not

  23. 23

    iOS8 - UILocalNotification, Check for permission later from code

  24. 24

    Condensing code: Check that multiple columns follow a boolean in data frame in R

  25. 25

    Can't get javascript working (beginner) - please check my code

  26. 26

    Javascript code to check URL content and keep only one thing

  27. 27

    I am trying to write a code but facing a java.lang.StringIndexOutOfBoundsException. Please check the code out

  28. 28

    How to cleanly check HTTP return code is in server side exception code family

  29. 29

    / usr / bin / parityとは何ですか?

ホットタグ

アーカイブ