Link to home
Start Free TrialLog in
Avatar of Software Software
Software SoftwareFlag for Austria

asked on

Set Even Parity

Have I done the exercise right or is something wrong?
User generated image
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>

uint32_t setEvenParity(uint32_t data);
uint8_t numberOfOnes(uint32_t data);
bool isEven(uint32_t data);

int main()
{
    printf("%d\n", setEvenParity(0));
    printf("%d\n", setEvenParity(1));
    printf("%d\n", setEvenParity(2));
    printf("%d\n", setEvenParity(3));
    printf("%d\n", setEvenParity(4));
    return 0;
}

uint32_t setEvenParity(uint32_t data){
    if(numberOfOnes == isEven(data)){
        return data || (0 << 31);
    }
        return data || (1 << 31);
}

uint8_t numberOfOnes(uint32_t data){
    uint8_t ctr=0;
    for(int i=1;i<=32;i++){
        if(data & (1<<i)){
            ctr++;
        }
    }
    return ctr;
}

bool isEven(uint32_t data){
    return !(data%2);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of noci
noci

Ozo's solution is right because: 
'||'   is logical or  and you need bitwise or:  '|'  or exclusive or: '^'