把一个数的比特位赋值为1或0

#include <stdio.h>
#include <stdlib.h>
//把num的第五个比特位赋为1
int main(){
	int num = 10;
	num = num | (1 << 5);
	printf("%d\n",num);
	system("pause");
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
//把num的第五个比特位赋为0
int main(){
	int num = 10;
	num = num & ~(1 << 5);
	printf("%d\n",num);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44781107/article/details/89929795