## 位运算符

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

C语言支持位运算符列在下表中。假设变量A=60和变量B=13，则：

& 二进制与操作位的结果，如果它存在于两个操作数 (A & B) =12, 也就是 0000 1100
| 二进制或操作副本位，如果它存在一个操作数中 (A | B) = 61, 也就是 0011 1101
^ 二进制异或操作副本，如果它被设置在一个操作数而不是两个比特 (A ^ B) = 49,  也就是 0011 0001
~ 二进制的补操作是一元的，具有“翻转”位的效果 (~A ) = -61, 也就是 1100 0011
<< 二进制左移位运算符。左边的操作数的值向左移动由右操作数指定的位数 A << 2 = 240 也就是 1111 0000
>> 二进制向右移位运算符。左边的操作数的值由右操作数指定的位数向右移动 A >> 2 = 15 也就是 0000 1111

## 例子

```#include

main()
{

unsigned int a = 60;	/* 60 = 0011 1100 */
unsigned int b = 13;	/* 13 = 0000 1101 */
int c = 0;

c = a & b;       /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d
", c );

c = a | b;       /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d
", c );

c = a ^ b;       /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d
", c );

c = ~a;          /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d
", c );

c = a << 2;     /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d
", c );

c = a >> 2;     /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d
", c );
}```

```Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15```