## page was renamed from Types Operators and Expressions/2.09 Bitwise Operators ## page was renamed from Types Operators and Expressions/2.9 Bitwise Operators <> == 2.9 Bitwise Operators 按位运算符 == C provides six operators for bit manipulation; these may only be applied to integral operands, that is, char, short, int, and long, whether signed or unsigned. {{{ & bitwise AND | bitwise inclusive OR ^ bitwise exclusive OR << left shift >> right shift ~ one's complement (unary) }}} C语言提供了6个位操作运算符。这些运算符只能作用于整型操作数,即只能作用于带符号或无符号的char、short、int与long类型:{{{ & 按位与(AND) | 按位或(OR) ^ 按位异或(XOR) << 左移 >> 右移 ~ 按位求反(一元运算符) }}} The bitwise AND operator & is often used to mask off some set of bits, for example {{{ n = n & 0177; }}} sets to zero all but the low-order 7 bits of n. 按位与运算符&经常用于屏蔽某些二进制位,例如:{{{ n = n & 0177; }}}该语句将n中除7个低二进制位外的其他各位均置为0。 The bitwise OR operator | is used to turn bits on: {{{ x = x | SET_ON; }}} sets to one in x the bits that are set to one in SET_ON. 按位或运算符常用于将某些二进制位置为1,例如:{{{ x = x | SET_ON; }}}该语句将x中对应于SET_ON中为1的那些二进制位置为1; The bitwise exclusive OR operator ^ sets a one in each bit position where its operands have different bits, and zero where they are the same. 按位异或运算符{{{^}}}当两个操作数的对应位不相同时将该位设置为1,否则,将该位设置为0。 One must distinguish the bitwise operators & and | from the logical operators && and ||, which imply left-to-right evaluation of a truth value. For example, if x is 1 and y is 2, then x & y is zero while x && y is one. 我们必须将位运算符&、|同逻辑运算符&&、||区分开来,后者用于从左至右求表达式的真值。例如,如果x的值为1,y的值为2,那么,x&y的结果为0,而x&&y的值为1。 The shift operators << and >> perform left and right shifts of their left operand by the number of bit positions given by the right operand, which must be non-negative. Thus x << 2 shifts the value of x by two positions, filling vacated bits with zero; this is equivalent to multiplication by 4. Right shifting an unsigned quantity always fits the vacated bits with zero. Right shifting a signed quantity will fill with bit signs ("arithmetic shift") on some machines and with 0-bits ("logical shift") on others. 移位运算符{{{<<}}}与{{{>>}}}分别用于将运算的左操作数左移与右移,移动的位数则由右操作数指定(右操作数的值必须是非负值)。因此,表达式{{{x<<2}}}将把x的值左移2位,右边空出的2位用0填补,该表达式等价于对左操作数乘以4。在对unsigned类型的无符号值进行右移位时,左边空出的部分将用0填补;当对signed类型的带符号值进行右移时,某些机器将对左边空出的部分用符号位填补(即“算术移位”),而另一些机器则对左边空出的部分用0填补(即“逻辑移位”)。 The unary operator ~ yields the one's complement of an integer; that is, it converts each 1-bit into a 0-bit and vice versa. For example {{{ x = x & ~077 }}} sets the last six bits of x to zero. Note that x & ~077 is independent of word length, and is thus preferable to, for example, x & 0177700, which assumes that x is a 16-bit quantity. The portable form involves no extra cost, since ~077 is a constant expression that can be evaluated at compile time. 一元运算符~用于求整数的二进制反码,即分别将操作数各二进制位上的1变为0,0变为1。例如:{{{ x = x & ~077 }}}将把x的最后6位设置为0。注意,表达式x&~077与机器字长无关,它比形式为x&0l77700的表达式要好,因为后者假定x是16位的数值。这种可移植的形式并没行增加额外开销,因为~077是常量表达式,可以在编译时求值。 As an illustration of some of the bit operators, consider the function getbits(x,p,n) that returns the (right adjusted) n-bit field of x that begins at position p. We assume that bit position 0 is at the right end and that n and p are sensible positive values. For example, getbits(x,4,3) returns the three bits in positions 4, 3 and 2, right-adjusted. {{{#!cplusplus /* getbits: get n bits from position p */ unsigned getbits(unsigned x, int p, int n) { return (x >> (p+1-n)) & ~(~0 << n); } }}} The expression x >> (p+1-n) moves the desired field to the right end of the word. ~0 is all 1-bits; shifting it left n positions with ~0<> (p+1-n)) & ~(~0 << n); } }}}其中,表达式x >> (p+1-n)将期望获得的字段移位到字的最右端。{{{~0}}}的所有位都为1,这里使用语句{{{~0<