C 按位运算符C Bitwise Operators

按位运算符执行按位“与”(&)、按位“异或”(^) 和按位“与或”(|) 运算。The bitwise operators perform bitwise-AND (&), bitwise-exclusive-OR (^), and bitwise-inclusive-OR (|) operations.

语法Syntax

AND-expressionAND-expression:
equality-expressionequality-expression

AND-expression & equality-expressionAND-expression & equality-expression

exclusive-OR-expressionexclusive-OR-expression:
AND-expressionAND-expression

exclusive-OR-expression ^ AND-expressionexclusive-OR-expression ^ AND-expression

inclusive-OR-expression:inclusive-OR-expression:
exclusive-OR-expressionexclusive-OR-expression

inclusive-OR-expression | exclusive-OR-expressioninclusive-OR-expression | exclusive-OR-expression

按位运算符的操作数必须具有整数类型,但其类型会不同。The operands of bitwise operators must have integral types, but their types can be different. 这些运算符执行常用算术转换;结果的类型是转换后操作数的类型。These operators perform the usual arithmetic conversions; the type of the result is the type of the operands after conversion.

C 按位运算符如下所述:The C bitwise operators are described below:

运算符Operator 描述Description
& 按位“与”运算符将其第一操作数的每个位与其第二操作数的相应位进行比较。The bitwise-AND operator compares each bit of its first operand to the corresponding bit of its second operand. 如果两个位均为 1,则对应的结果位将设置为 1。If both bits are 1, the corresponding result bit is set to 1. 否则,将对应的结果位设置为 0。Otherwise, the corresponding result bit is set to 0.
^ 按位“异或”运算符将其第一操作数的每个位与其第二操作数的相应位进行比较。The bitwise-exclusive-OR operator compares each bit of its first operand to the corresponding bit of its second operand. 如果一个位是 0,另一个位是 1,则相应的结果位将设置为 1。If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. 否则,将对应的结果位设置为 0。Otherwise, the corresponding result bit is set to 0.
|| 按位“与或”运算符将其第一操作数的每个位与第二操作数的相应位进行比较。The bitwise-inclusive-OR operator compares each bit of its first operand to the corresponding bit of its second operand. 如果其中一个位是 1,则将对应的结果位设置为 1。If either bit is 1, the corresponding result bit is set to 1. 否则,将对应的结果位设置为 0。Otherwise, the corresponding result bit is set to 0.

示例Examples

这些声明用于以下三个示例:These declarations are used for the following three examples:

short i = 0xAB00;  
short j = 0xABCD;  
short n;  

n = i & j;  

第一个示例中的分配给 n 的结果与 i 相同(0xAB00 十六进制)。The result assigned to n in this first example is the same as i (0xAB00 hexadecimal).

n = i | j;  

n = i ^ j;  

第二个示例中的按位“与或”生成值 0xABCD(十六进制),而第三个示例中的按位“异或”生成 0xCD(十六进制)。The bitwise-inclusive OR in the second example results in the value 0xABCD (hexadecimal), while the bitwise-exclusive OR in the third example produces 0xCD (hexadecimal).

Microsoft 专用Microsoft Specific

根据 ANSI C 标准,对有符号整数进行的按位运算的结果是实现定义的。The results of bitwise operation on signed integers is implementation-defined according to the ANSI C standard. 对于 Microsoft C 编译器,对有符号整数进行的按位运算与对无符号整数进行的按位运算的工作原理相同。For the Microsoft C compiler, bitwise operations on signed integers work the same as bitwise operations on unsigned integers. 例如,-16 & 99 可用二进制格式表示For example, -16 & 99 can be expressed in binary as

  11111111 11110000  
& 00000000 01100011  
  _________________  
  00000000 01100000  

按位 AND 的结果为 96(十进制)。The result of the bitwise AND is 96 decimal.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

按位“与”运算符:& Bitwise AND Operator: &
按位“异或”运算符:^ Bitwise Exclusive OR Operator: ^
按位“与或”运算符:|Bitwise Inclusive OR Operator: |