首页 > 代码库 > 寄存器中 置位 与 清除 -- C

寄存器中 置位 与 清除 -- C

第3位的置位与清除操作。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BIT3 (0x1<<3)

int
main()
{
	int a = 0x00000000;

	printf("a = 0x%08X\n", a);
	/*
		置位第3bit
		a = a | (0x1<<3)
		a = a | 0x8
	*/
	a |= BIT3;
	printf("a = 0x%08X\n", a);
	
	/*
		清楚第3bit
		a = a & ~ (0x1<<3)
		a = a & ~ 0x8
		a = a & 0xfffffff7
	*/
	a &= ~BIT3;//
	printf("a = 0x%08X\n", a);
}
/*
[root@localhost test_class]# gcc quote.cpp ;./a.out
a = 0x00000000
a = 0x00000008
a = 0x00000000
*/