Sunday, 23 June 2013

Parity Bit for 7-bit Integer.


#include <iostream>
using namespace std;

int setParity(int n)
{
     int number = 1;
     number = number<<7 ;
     return (n|number);
}

int countOnes(int n)
{
    int number = 1;
    int count = 0;
    for(int i=0; i<7; i++)
    {
            if(number & n)
            {
                      count++;
            }
            number = number<<1;
    }
    return count;
}

int main()
{
    int n = 11;
    if(countOnes(n)%2 ==1)
        n = setParity(n);
    cout<<n;
    system("pause");
}


No comments:

Post a Comment