在C ++中找到奇偶校验的程序
在本教程中,我们将讨论一个寻找奇偶校验的程序。
为此,我们将提供一个号码。我们的任务是找到它的奇偶性,即计数个数是奇数还是偶数。
示例
# include<bits/stdc++.h>
# define bool int
using namespace std;
//查找给定数字的奇偶性
bool getParity(unsigned int n) {
bool parity = 0;
while (n){
parity = !parity;
n = n & (n - 1);
}
return parity;
}
int main() {
unsigned int n = 7;
cout<<"Parity of no "<<n<<": "<<(getParity(n)? "Odd": "even");
getchar();
return 0;
}输出结果
Parity of no 7: odd
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬