c语言中的运算符及其含义
1)&(按位与) (1) & (bitwise AND))
It does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
它对两个数字的每一位进行“与”运算。 仅当两个位均为1时,AND的结果才为1。
Example:
例:
4 & 7
4 → 00000100
7 → 00000111
Doing AND for each bit
From LSB:
0 & 1= 0 (LSB of output)
0 & 1= 0
1 & 1= 1
0 & 0 =0
0 & 0 =0
0 & 0 =0
0 & 0 =0
0 & 0 =0
Thus output:
00000100 → 4
4 & 7 =4
2)| (按位或) (2) | (bitwise OR))
It takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
它使用两个数字作为操作数,并对两个数字的每一位进行“或”运算。 OR的结果为1,两个位中的任何一个为1。
Example:
例:
4 | 7
4 → 00000100
7 → 00000111
Doing OR for each bit
From LSB:
0 | 1 =1 (LSB of output)
0 | 1 =1
1 | 1 =1
0 | 0 =0
0 | 0 =0
0 | 0 =0
0 | 0 =0
0 | 0 =0
Thus output:
00000111 → 7
4 | 7 =7
3)^(按位XOR) (3) ^ (bitwise XOR))
It does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
它对两个数字的每一位执行XOR。 如果两个位不同,则XOR的结果为1。
Example:
例:
4 ^ 7
4 → 00000100
7 → 00000111
Doing XOR for each bit
From LSB:
0 ^ 1 =1 (LSB of output)
0 ^ 1 =1
1 ^ 1 =0
0 ^ 0 =0
0 ^ 0 =0
0 ^ 0 =0
0 ^ 0 =0
0 ^ 0 =0
Thus output:
00000011 → 3
4 ^ 7 =3
4)<<(左移) (4) << (left shift))
It takes two operands, left shifts the bits of the first operand, the second operand decides the number of places to shift. In every left shift all bits are shifted to left adding a logical 0 at LSB.
它需要两个操作数,左移第一个操作数的位,第二个操作数确定要移位的位数。 在每个左移中,所有位都向左移,在LSB处加逻辑0。
Example:
例:
4<<1
Before 1 left shift
00000100
After 1 left shift
00001000 → 8
So 4<<1 = 8
5)>>(右移) (5) >> (right shift))
It takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
它需要两个数字,右移第一个操作数的位,第二个操作数确定要移位的位数。
4>>1
Before 1 right shift
00000100
After 1 right shift
00000010 → 2
So 4<<1 = 2
6)〜(按位非) (6) ~ (bitwise NOT))
It takes one operand and inverts all bits of it
它需要一个操作数并将其所有位求反
Example:
例:
~4
00000100 → 11111011
~4=251
翻译自: https://www.includehelp.com/c/bitwise-operators-and-their-working-with-examples-in-c.aspx
c语言中的运算符及其含义