Software Software
asked on
Set Even Parity
Have I done the exercise right or is something wrong?
#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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
'||' is logical or and you need bitwise or: '|' or exclusive or: '^'