本文介绍了如何检查值是否具有偶数奇偶校验或奇数校验?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果一个值具有偶数个1位,则它具有偶数奇偶校验.如果值的奇数为1位,则它具有奇数奇偶校验.例如,0110
具有偶校验,而1110
具有奇校验.
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. For example, 0110
has even parity, and 1110
has odd parity.
如果x
具有偶校验,我必须返回1
.
I have to return 1
if x
has even parity.
int has_even_parity(unsigned int x) {
return
}
推荐答案
尝试:
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;
}
瓦尔特
这篇关于如何检查值是否具有偶数奇偶校验或奇数校验?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!