Home > Mobile >  How do you correct the middle bit using parity error correction
How do you correct the middle bit using parity error correction

Time:06-15

uint8_t parityDecoder(uint8_t byte)
{
    uint8_t p2Circle, p1Circle, p0Circle;
    uint8_t d3, d2, d1, d0, p2, p1, p0;
    
    // Putting each bit to a variable
    p0 = (byte & BIT0);
    p1 = (byte & BIT1) >> 1;
    p2 = (byte & BIT2) >> 2;

    d0 = (byte & BIT3) >> 3;
    d1 = (byte & BIT4) >> 4;
    d2 = (byte & BIT5) >> 5;
    d3 = (byte & BIT6) >> 6;

    // Check if the 3 data bits on one side is odds or even
    p0Circle = (d0   d1   d2) % 2;
    p1Circle = (d0   d1   d3) % 2;
    p2Circle = (d1   d2   d3) % 2;

    // pinpoints which data bit is toggled by comparing with a parity bit
    if ((p0Circle != p0) & (p1Circle != p1))
    {
        d0 ^= 1;
    }

    if ((p0Circle != p0) & (p2Circle != p2))
    {
        d2 ^= 1;
    }

    if ((p1Circle != p1) & (p2Circle != p2))
    {
        d3 ^= 1;
    }

    // Not working
    if ((p0Circle != p0) && (p1Circle != p1) && (p2Circle != p2))
    {
        d1 ^= 1;
    }

    return (((((((byte << 8) | (d3 << 6)) | (d2 << 5)) | (d1 << 4)) | (d0 << 3)) | (p2 << 2)) | (p1 << 1)) | p0;
}

So I have a parity error correction function. what this function does is when there is a byte and one of its bits is toggled, it will toggle it back to normal again.

My problem is when all 3 circles are not equal then d1 should toggle but in this case it does something else.

I tried testing the function and this is the output

expectedByte = 0b00100101; // decimal 37
inputByte = 0b0 0110 101; // decimal 53 because 5th bit is toggled

The output of this function is 77 which is 0b01001101

I can't see where the error in my calculations is and in need of help.

CodePudding user response:

Note that when all 3 circles are not equal, the all pairs of 2 circles are also not equal.

You can prevent bit flips for pairs of 2 circles using else.

    if ((p0Circle != p0) && (p1Circle != p1) && (p2Circle != p2))
    {
        d1 ^= 1;
    }
    else
    {
        // pinpoints which data bit is toggled by comparing with a parity bit
        if ((p0Circle != p0) & (p1Circle != p1))
        {
            d0 ^= 1;
        }

        if ((p0Circle != p0) & (p2Circle != p2))
        {
            d2 ^= 1;
        }

        if ((p1Circle != p1) & (p2Circle != p2))
        {
            d3 ^= 1;
        }
    }
  •  Tags:  
  • c
  • Related