欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++工作笔记-对"|","&"这些位运算的进一步认识

程序员文章站 2022-03-21 23:48:55
...

程序运行截图如下:

C++工作笔记-对"|","&"这些位运算的进一步认识

源码如下:

#include <cstdio>

#define A 0x00000001
#define B 0x00000100
#define C 0x00010000
#define D 0x01000000

#define E 0x00000001

#define AorB A&B

void main() {

	int valueA = AorB;
	printf("valueA is 0x%08x\n", valueA);

	int valueB = A | B;
	printf("valueB is 0x%08x\n", valueB);

	int valueC = B | C | D;
	printf("valueC is 0x%08x\n", valueC);

	int valueD = A | C | D;
	printf("valueD is 0x%08x\n", valueD);

	if (valueA&E) {
		printf("valueA&E is successful!\n");
	}
	else {
		printf("valueA&E is failed!\n");
	}

	if (valueB&E) {
		printf("valueB&E is successful!\n");
	}
	else {
		printf("valueB&E is failed!\n");
	}

	if (valueC&E) {
		printf("valueC&E is successful!\n");
	}
	else {
		printf("valueC&E is failed!\n");
	}

	if (valueD&E) {
		printf("valueD&E is successful!\n");
	}
	else {
		printf("valueD&E is failed!\n");
	}

	getchar();
}

 

总结:一般用于带有status或flag这些地方,比如一个窗口包含了3种状态(这时候可以用'|"),当想判断这一群状态里面是否还有某一状态时这个时候就可以用“&”

相关标签: C