Sunday, 23 June 2013

Checking Parity.


#include <iostream>
using namespace std;

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

bool isParitySet(int n)
{
     int number = 128;
     if(number && n)
         return true;
     else
         return false;
}

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 = 2;
    if(countOnes(n) % 2 == 0)
    {
        if(isParitySet(n))
        {
             n = n ^ 128;;
        }
        cout<<n;
    }
    else
    {
        cout<<"INVALLID NUMBER";
    }
    system("pause");
}



No comments:

Post a Comment