C 位運算符

2018-07-05 14:08 更新

學習C - C位運算符

位運算符對整數(shù)值的位進行操作。

有六個位運算符,如下表所示。

操作符描述
&按位與運算符
|按位或運算符
^按位異或運算符
~按位取反運算符,也稱為1的補碼運算符
>>按位右移運算符
<<按位左移運算符

所有這些僅對整數(shù)類型進行操作。

?運算符是一元運算符 - 它適用于一個操作數(shù) - 其他是二元運算符。


位與

按位與運算符&,以這樣的方式組合其操作數(shù)的相應位,如果兩個位都為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;
    }

上面的代碼生成以下結果。

位翻轉

一元運算符?翻轉其操作數(shù)的位,所以1變?yōu)?,0變?yōu)?。

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

執(zhí)行此語句后,z將具有值14.位設置如下:

x          0       0       0      0      1       1       0       1

~x          1      1       1      1      0       0        1      0

值1111 0010是負整數(shù)的二進制補碼表示中的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;
    }

上面的代碼生成以下結果。

位左移

移位操作符將左操作數(shù)中的位移動由右操作數(shù)指定的位數(shù)。

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

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,右移的結果取決于您的系統(tǒng)。

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;

效果是將整數(shù)變量的內容移位,值左移四位。

與以下內容完全相同:

value = value << 4;

此示例說明如何使用掩碼從變量中選擇多個位。

您將編寫一個在變量中設置值的程序,然后使用按位運算符來反轉十六進制數(shù)字的順序。


    #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

公眾號
微信公眾號

編程獅公眾號