位与运算符

位与运算符

#include <iostream>
using namespace std;

int main()
{
	//1.位与运算符
	int a = 0b0011; //3
	int b = 0b1011;	//11
	//		0b0011	//3
	cout << (a & b) << endl;
	cout << "----" << endl;

	//2.奇偶性
	//如果一个数 & 1为1则为奇数,否则为偶数
	//0b0101 0b0001
	cout << (5 & 1) << endl;
	cout << "----" << endl;
	
	//3.获取一个数二进制的末五位
	//& 0b11111
	int c = 0b0001010111;
	cout << (c & 0b11111) << endl;
	cout << "----" << endl;
	
	//4.将末五位归零
	int d = 0b11111111111111111111111111100000;
	cout << (c & d) << endl;
	cout << "----" << endl;

	//5.消除末尾连续的1
	//& x-1
	int x = 0b101000111111;
	cout << (x & (x + 1)) << endl;
	cout << "----" << endl;

	//6.2的幂判定
	//& y+1,如果是则结果为0否则为1
	int y = 32;
	cout << (y & (y - 1)) << endl;
	cout << "----" << endl;

	return 0;
}
posted @ 2026-01-26 20:15  wind_one  阅读(1)  评论(0)    收藏  举报