C 位运算符

2018-07-05 14:08 更新

学习C - C位运算符

位运算符对整数值的位进行操作。

有六个位运算符,如下表所示。

操作符描述
&按位与运算符
|按位或运算符
^按位异或运算符
~按位取反运算符,也称为1的补码运算符
>>按位右移运算符
<<按位左移运算符

所有这些仅对整数类型进行操作。

运算符是一元运算符 - 它适用于一个操作数 - 其他是二元运算符。


位与

按位与运算符&,以这样的方式组合其操作数的相应位,如果两个位都为1,则结果位为1;否则,结果位为0。

假设你声明以下变量:

int x = 13;
int y = 6;
int z = x & y;                     // AND corresponding bits of x and y

在第三个语句之后,z将具有值4(二进制100)。

这是因为x和y中的相应位组合如下:

x           0      0     0      0     1     1      0     1
y           0      0     0      0     0     1      1     0
x & y       0      0     0      0     0     1      0     0

例子


    #include <stdio.h>
    int main(void)
    {
        int x = 13;
        int y = 6;
        int z = x & y;                     // AND corresponding bits of x and y

        printf("\n original = %X", x);
        printf("\n original = %X", y);

        printf("\t result = %X\n", z);
      return 0;
    }

上面的代码生成以下结果。


位或

按位或运算符 | ,如果相应位中的一个或两个位为1,则导致1;否则,结果为0。

如果使用|组合相同的x和y值运算符在这样的声明中:

int x = 13;
int y = 6;

int z = x | y;                     // OR the bits of x and y
x           0       0       0      0       1       1        0       1
y           0       0       0      0       0       1        1       0
x | y       0       0       0      0       1       1        1       1

因此,存储在z中的值为15(二进制1111)。

例子


    #include <stdio.h>
    int main(void)
    {
        int x = 13;
        int y = 6;
        int z = x | y;                     // OR the bits of x and y

        printf("\n original = %X", x);
        printf("\n original = %X", y);

        printf("\t result = %X\n", z);
      return 0;
    }

上面的代码生成以下结果。

位异或

按位异或运算符 ^ ,如果两个位都不同,则产生1,如果它们相同,则产生0。

int x = 13;
int y = 6;

int z = x ^ y;                     // Exclusive OR the bits of x and y
<myPreCode>

<p>This results in z containing the value 11 (binary 1011), because the bits combine as follows: </p>

<myPreCode>
x          0       0       0      0      1       1       0       1
y          0       0       0      0      0       1       1       0
x ^ y      0       0       0      0      1       0       1       1

例子


    #include <stdio.h>
    int main(void)
    {
        int x = 13;
        int y = 6;
        int z = x ^ y;                     // Exclusive OR the bits of x and y

        printf("\n original = %X", x);
        printf("\n original = %X", y);

        printf("\t result = %X\n", z);
      return 0;
    }

上面的代码生成以下结果。

位翻转

一元运算符翻转其操作数的位,所以1变为0,0变为1。

int x = 13;
int z = ~x;                            // Store 1"s complement of x

执行此语句后,z将具有值14.位设置如下:

x          0       0       0      0      1       1       0       1

~x          1      1       1      1      0       0        1      0

值1111 0010是负整数的二进制补码表示中的14。

例子


    #include <stdio.h>
    int main(void)
    {
        int x = 13;
        int z = ~x;                            // Store 1"s complement of x

        printf("\n original = %X", x);

        printf("\t result = %X\n", z);

        return 0;
    }

上面的代码生成以下结果。

位左移

移位操作符将左操作数中的位移动由右操作数指定的位数。

您可以使用以下语句指定左移操作:

int value = 12;
int shiftcount = 3;                    // Number of positions to be shifted
int result = value << shiftcount;      // Shift left shiftcount positions

这些位移动到左侧三个位置,在右侧引入0。

例子


    #include <stdio.h>
    int main(void)
    {
        int value = 12;
        int shiftcount = 3;                    // Number of positions to be shifted
        int result = value << shiftcount;      // Shift left shiftcount positions

        printf("\t result = %X\n", result);

        return 0;
    }

上面的代码生成以下结果。

无符号右移

右移位操作符将位向右移动。对于无符号值,左侧引入的位用零填充。

unsigned int value = 65372U;
unsigned int result = value >> 2;      // Shift right two bits

值的位将向右移动两个位置,在左端引入零。

例子


    #include <stdio.h>
    int main(void)
    {
        unsigned int value = 65372U;
        unsigned int result = value >> 2;      // Shift right two bits

        printf("\t result = %X\n", result);

        return 0;
    }

上面的代码生成以下结果。

带符号右移

对于负值的带符号值,最左边的位将为1,右移的结果取决于您的系统。

int new_value = -164;
int new_result = new_value >> 2;       // Shift right two bits

这将把new_value中的值向右移位两位,结果将存储在 new_result 中。

例子


    #include <stdio.h>
    int main(void)
    {
        int new_value = -164;
        int new_result = new_value >> 2;       // Shift right two bits

        printf("\t result = %X\n", new_result);

        return 0;
    }

上面的代码生成以下结果。

op= 使用按位运算符

您可以使用op= 赋值形式中的所有二进制按位运算符。

异常是运算符,它是一元运算符。

lhs op= rhs;

相当于语句:

lhs = lhs op (rhs);

这意味着如果你写:

value <<= 4;

效果是将整数变量的内容移位,值左移四位。

与以下内容完全相同:

value = value << 4;

此示例说明如何使用掩码从变量中选择多个位。

您将编写一个在变量中设置值的程序,然后使用按位运算符来反转十六进制数字的顺序。


    #include <stdio.h>
    int main(void)
    {
      unsigned int original = 0xAAA;
      unsigned int result = 0;
      unsigned int mask = 0xF;          // Rightmost four bits

      printf("\n original = %X", original);

      // Insert first digit in result
      result |= original & mask;        // Put right 4 bits from original in result

      // Get second digit
      original >>= 4;                   // Shift original right four positions
      result <<= 4;                     // Make room for next digit
      result |= original & mask;          // Put right 4 bits from original in result

      /* Get third digit */
      original >>= 4;                   // Shift original right four positions
      result <<= 4;                     // Make room for next digit
      result |= original & mask;        // Put right 4 bits from original in result
      printf("\t result = %X\n", result);
      return 0;
    }

上面的代码生成以下结果。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号