C语言的字节运算符  

Table 5-11. Boolean bitwise operators
Operator Meaning Example Result (for each bit position)

(1 = set, 0 = cleared)

& Bitwise AND x & y 1, if 1 in both x and y

0, if 0 in x or y, or both

| Bitwise OR x | y 1, if 1 in x or y, or both

0, if 0 in both x and y

^ Bitwise

exclusive OR

x ^ y 1, if 1 either in x or in y, but not in both

0, if either value in both x and y

~ Bitwise NOT

(one’s complement )

~x 1, if 0 in x

0, if 1 in x

 

The operands of the bitwise operators must have integer types, and are subject to the usual arithmetic conversions. The resulting common type of the operands is the type of the result. Table 5-12 illustrates the effects of these operators.

Table 5-12. Effects of the bitwise operators
Expression (or declaration) Bit pattern
int a = 6; 0 ... 0 0 1 1 0
int b = 11; 0 ... 0 1 0 1 1
a & b 0 ... 0 0 0 1 0
a | b 0 ... 0 1 1 1 1
a ^ b 0 ... 0 1 1 0 1
~a 1 ... 1 1 0 0 1