Cprogramming 简明教程
Bitwise Operators in C
C 语言中的按位运算符允许对存储在计算机内存中的数据的底层运算。
Bitwise operators in C allow low-level manipulation of data stored in computer’s memory.
按位运算符与 C 语言中的逻辑运算符形成对比。例如,逻辑 AND 运算符 ( && ) 对两个布尔表达式执行 AND 运算,而按位 AND 运算符 ( & ) 对两个操作数的每个对应位执行 AND 运算。
Bitwise operators contrast with logical operators in C. For example, the logical AND operator (&&) performs AND operation on two Boolean expressions, while the bitwise AND operator (&) performs the AND operation on each corresponding bit of the two operands.
对于三个逻辑运算符 && 、 || 和 ! ,C 语言中对应的按位运算符为 & 、 | 和 ~ 。
For the three logical operators &&, ||, and !, the corresponding bitwise operators in C are &, | and ~.
此外,符号 ^ (异或)、 << (左移)和 >> (右移)是其他按位运算符。
Additionally, the symbols ^ (XOR), << (left shift) and >> (right shift) are the other bitwise operators.
Operator |
Description |
Example |
& |
Binary AND Operator copies a bit to the result if it exists in both operands. |
(A & B) |
Binary OR Operator copies a bit if it exists in either operand. |
||
(A |
B) |
^ |
Binary XOR Operator copies the bit if it is set in one operand but not both. |
(A ^ B) |
~ |
Binary One’s Complement Operator is unary and has the effect of 'flipping' bits. |
(~A |
<< |
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. |
A << 2 |
>> |
尽管这些运算符对单个位执行操作,但它们需要操作数采用 C 数据类型或变量的形式,因为变量在内存中占据特定的字节数。
Even though these operators work on individual bits, they need the operands in the form C data types or variables only, as a variable occupies a specific number of bytes in the memory.
Bitwise AND Operator (&) in C
按位 AND (&) 运算符根据以下真值表执行操作 −
The bitwise AND (&) operator performs as per the following truth table −
bit |
bit |
a & b |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
按位二进制 AND 对二进制形式的数字中每个位置的位执行逻辑运算。
Bitwise binary AND performs logical operation on the bits in each position of a number in its binary form.
假设两个 int 变量“a”和“b”的值为 60(相当于二进制中的 0011 1100)和 13(相当于二进制中的 0000 1101),则“a & b”运算会产生 13,按照如下所示的对应位的按位 AND。
Assuming that the two int variables "a" and "b" have the values 60 (equivalent to 0011 1100 in binary) and 13 (equivalent to 0000 1101 in binary), the "a & b" operation results in 13, as per the bitwise ANDing of their corresponding bits illustrated below −
0011 1100
& 0000 1101
---------
= 0000 1100
二进制数 00001100 等于十进制中的 12。
The binary number 00001100 corresponds to 12 in decimal.
Bitwise OR (|) Operator
按位 OR(|)运算符按照以下真值表执行 −
The bitwise OR (|) operator performs as per the following truth table −
bit |
bit |
a |
b |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
按位二进制 OR 在一个数的二进制形式中,对每个位置里的位执行逻辑运算。
Bitwise binary OR performs logical operation on the bits in each position of a number in its binary form.
假设两个 int 变量“a”和“b”的值分别是 60(等于二进制中的 0011 1100)和 13(等于二进制中的 0000 1101),则“ a | b ”的结果为 61,按其对应位的按位 OR 所得,如下所示 −
Assuming that the two int variables "a" and "b" have the values 60 (equivalent to 0011 1100 in binary) and 13 (equivalent to 0000 1101 in binary), then "a | b" results in 61, as per the bitwise OR of their corresponding bits illustrated below −
0011 1100
| 0000 1101
---------
= 0011 1101
二进制数 00111101 等于十进制中的 61。
The binary number 00111101 corresponds to 61 in decimal.
Bitwise XOR (^) Operator
按位 XOR(^)运算符按照以下真值表执行 −
The bitwise XOR (^) operator performs as per the following truth table −
bit |
bit |
a ^ b |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
按位二进制 XOR 在一个数的二进制形式中,对每个位置里的位执行逻辑运算。XOR 运算称为“异或”。
Bitwise binary XOR performs logical operation on the bits in each position of a number in its binary form. The XOR operation is called "exclusive OR".
Note: 只有当其中一个运算元为 1 时,XOR 的结果才为 1。与 OR 不同,如果两个位都是 1,则 XOR 的结果为 0。
Note: The result of XOR is 1 if and only if one of the operands is 1. Unlike OR, if both bits are 1, XOR results in 0.
假设两个 int 变量“a”和“b”的值分别是 60(等于二进制中的 0011 1100)和 13(等于二进制中的 0000 1101),则“ a ^ b ”运算的结果为 49,按其对应位的按位 XOR 所得,如下所示 −
Assuming that the two int variables "a" and "b" have the values 60 (equivalent to 0011 1100 in binary) and 13 (equivalent to 0000 1101 in binary), the "a ^ b" operation results in 49, as per the bitwise XOR of their corresponding bits illustrated below −
0011 1100
^ 0000 1101
---------
= 0011 0001
二进制数 00110001 等于十进制中的 49。
The binary number 00110001 corresponds to 49 in decimal.
The Left Shift (<<) Operator
左移运算符由 << 符号表示。它按照右操作数指定的位数,将其左操作数中的每个位向左移动。移动时产生的任何空白由零填充。
The left shift operator is represented by the << symbol. It shifts each bit in its left-hand operand to the left by the number of positions indicated by the right-hand operand. Any blank spaces generated while shifting are filled up by zeroes.
假设 int 变量“a”的值为 60(等于二进制中的 0011 1100),则“ a << 2 ”运算的结果为 240,按其对应位的按位左移所得,如下所示 −
Assuming that the int variable "a" has the value 60 (equivalent to 0011 1100 in binary), the "a << 2" operation results in 240, as per the bitwise left-shift of its corresponding bits illustrated below −
0011 1100 << 2 = 1111 0000
二进制数 11110000 等于十进制中的 240。
The binary number 11110000 corresponds to 240 in decimal.
The Right Shift (>>) Operator
右移运算符由 >> 符号表示。它按照右操作数指定的位数,将其左操作数中的每个位向右移动。移动时产生的任何空白由零填充。
The right shift operator is represented by the >> symbol. It shifts each bit in its left-hand operand to the right by the number of positions indicated by the right-hand operand. Any blank spaces generated while shifting are filled up by zeroes.
假设 int 变量 a 的值为 60(等于二进制中的 0011 1100),则“ a >> 2 ”运算的结果为 15,按其对应位的按位右移所得,如下所示 −
Assuming that the int variable a has the value 60 (equivalent to 0011 1100 in binary), the "a >> 2" operation results in 15, as per the bitwise right-shift of its corresponding bits illustrated below −
0011 1100 >> 2 = 0000 1111
二进制数 00001111 等于十进制中的 15。
The binary number 00001111 corresponds to 15 in decimal.
The 1’s Complement (~) Operator
C 中的 1 的补运算符 (~) 是一元运算符,只接受一个操作元。其作用是“翻转”位,这意味着 1 由 0 代替,反之亦然。
The 1’s compliment operator (~) in C is a unary operator, needing just one operand. It has the effect of "flipping" the bits, which means 1s are replaced by 0s and vice versa.
a |
~a |
0 |
1 |
1 |
0 |
假设 int 变量“a”的值为 60(等于二进制中的 0011 1100),则“ ~a ”运算的结果为 2 的补码形式中的 -61,按其对应位的按位右移所得,如下所示 −
Assuming that the int variable "a" has the value 60 (equivalent to 0011 1100 in binary), then the "~a" operation results in -61 in 2’s complement form, as per the bitwise right-shift of its corresponding bits illustrated below −
~ 0011 1100 = 1100 0011
二进制数 1100 0011 等于十进制中的 -61。
The binary number 1100 0011 corresponds to -61 in decimal.
Example
在本例中,我们强调了所有按位运算符的操作:
In this example, we have highlighted the operation of all the bitwise operators:
#include <stdio.h>
int 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\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
return 0;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
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