Manuel - 5 months ago 27

C Question

A value has even parity if it has an even number of 1 bits. A value has an odd parity if it has an odd number of 1 bits. Well For example, 0110 has even parity, and 1110 has odd parity. I have to return 1 iff x has even parity.

Got stuck....Any ideas???Any help will appreciated. Thanks. Also if it very simple question the sorry..Very beginner here

`int has_even_parity(unsigned int x) {`

return

}

Answer

Try:

```
int has_even_parity(unsigned int x){
unsigned int count = 0, i, b = 1;
for(i = 0; i < 32; i++){
if( x & (b << i) ){count++;}
}
if( (count % 2) ){return 0;}
return 1;
}
```

valter