二进制位运算目的是提高运算速度
- 位运算只能整数
- 除2
// a/2
int a = 16;
a /= 2; // 8
a = a >> 1; // 4
cout << a << endl;
- 判断奇偶
// 判断奇偶
int b = 16;
if (b % 2 == 0) {
cout << "b是偶数" << endl;
} else {
cout << "b是奇数" << endl;
}
if ((b & 1) == 0) {
cout << "b是偶数" << endl;
} else {
cout << "b是奇数" << endl;
}
- 2的n次方
// 2^n
int c = 5;
cout << pow(2, c) << endl; // 32
cout << (1 << c) << endl; // 32
标签:奇偶,场景,运算,16,二进制位,int,cout
From: https://www.cnblogs.com/xiufanivan/p/17409543.html