C 语言中的按位运算符

在算术逻辑单元(位于CPU内部)中,加法、减法、乘法和除法等数学运算是以位级别进行的。要在C语言编程中执行位级别操作,需要使用位运算符。

运算符 运算符的含义
& 按位与
| 按位或
^ 按位异或
~ 位补码
左移
>> 右移

按位AND运算符 &

当两个操作数的对应位都为1时,按位AND的输出为1。如果操作数的任何一位为0,则对应位的计算结果为0

在C语言编程中,按位AND运算符用&表示。

假设对整数1225执行按位AND运算。

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25
  00001100
& 00011001
  ________
  00001000  = 8 (In decimal)

示例1:按位AND

#include <stdio.h>

int main() {

    int a = 12, b = 25;
printf("Output = %d", a & b);
return 0; }

输出

Output = 8

按位OR运算符 |

当两个操作数的至少一个对应位为1时,按位OR的输出为1。在C语言编程中,按位OR运算符用|表示。

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25
  00001100
| 00011001
  ________
  00011101  = 29 (In decimal)

示例2:按位OR

#include <stdio.h>

int main() {

    int a = 12, b = 25;
printf("Output = %d", a | b);
return 0; }

输出

Output = 29

按位XOR(异或)运算符 ^

当两个操作数的对应位相反时,按位XOR运算符的结果为1。它用^表示。

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25
  00001100
^ 00011001
  ________
  00010101  = 21 (In decimal)

示例3:按位XOR

#include <stdio.h>

int main() {

    int a = 12, b = 25;
printf("Output = %d", a ^ b);
return 0; }

输出

Output = 21

按位补码运算符 ~

按位补码运算符是单目运算符(只作用于一个操作数)。它将1变为0,将0变为1。它用~表示。

35 = 00100011 (In Binary)

Bitwise complement Operation of 35
~ 00100011 
  ________
  11011100  = 220 (In decimal)

C语言编程中按位补码运算符的曲折之处

35~35)的按位补码是-36而不是220,这是为什么?

对于任何整数nn的按位补码将是-(n + 1)。要理解这一点,您需要了解2的补码。

2 的补码

二进制数的补码运算。一个数的2的补码等于该数的补码加1。例如

 Decimal         Binary           2's complement 
   0            00000000           -(11111111+1) = -00000000 = -0(decimal)
   1            00000001           -(11111110+1) = -11111111 = -256(decimal)
   12           00001100           -(11110011+1) = -11110100 = -244(decimal)
   220          11011100           -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2's complement.

35的按位补码是220(十进制)。220的2的补码是-36。因此,输出是-36而不是220

任何数N的按位补码是-(N+1)。方法如下

bitwise complement of N = ~N (represented in 2's complement form)
2'complement of ~N= -(~(~N)+1) = -(N+1)

示例4:按位补码

#include <stdio.h>

int main() {

printf("Output = %d\n", ~35); printf("Output = %d\n", ~-12);
return 0; }

输出

Output = -36
Output = 11

C语言编程中的移位运算符

C语言编程中有两个移位运算符

  • 右移运算符
  • 左移运算符。

右移运算符

右移运算符将所有位向右移动指定的位数。它用>>表示。

212 = 11010100 (In binary)
212 >> 2 = 00110101 (In binary) [Right shift by two bits]
212 >> 7 = 00000001 (In binary)
212 >> 8 = 00000000 
212 >> 0 = 11010100 (No Shift)

左移运算符

左移运算符将所有位向左移动指定的位数。左移运算符产生的空位会被0填充。左移运算符的符号是<<

212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 = 11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)

示例#5:移位运算符

#include <stdio.h>

int main() {

    int num=212, i;

    for (i = 0; i <= 2; ++i) {
printf("Right shift by %d: %d\n", i, num >> i);
} printf("\n"); for (i = 0; i <= 2; ++i) {
printf("Left shift by %d: %d\n", i, num << i);
} return 0; }
Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53

Left Shift by 0: 212
Left Shift by 1: 424
Left Shift by 2: 848
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战