【學習筆記】【C語言】位元運算,學習筆記運算

來源:互聯網
上載者:User

【學習筆記】【C語言】位元運算,學習筆記運算
1. & 按位與

1> 功能

只有對應的兩個二進位均為1時,結果位才為1,否則為0。

2> 舉例: 比如9&5,其實就是1001&101=1,因此9&5=1

3> 規律

二進位中,與1相&就保持原位,與0相&就為0

2. | 按位或

1> 功能

只要對應的二個二進位有一個為1時,結果位就為1,否則為0。

2> 舉例: 比如9|5,其實就是1001|101=1101,因此9|5=13

3. ^ 按位異或

1> 功能

當對應的二進位相異(不相同)時,結果為1,否則為0。

2> 舉例: 比如9^5,其實就是1001^101=1100,因此9^5=12

3> 規律

相同整數相^的結果是0。比如5^5=0

多個整數相^的結果跟順序無關。比如5^6^7=5^7^6

因此得出結論:a^b^a = b

4. ~ 取反

對整數a的各二進位進行取反,符號位也取反(0變1,1變0)

 

5. << 左移

把整數a的各二進位全部左移n位,高位丟棄,低位補0。左移n位其實就是乘以2的n次方

由於左移是丟棄最高位,0補最低位,所以符號位也會被丟棄,左移出來的結果值可能會改變正負性

6. >> 右移

把整數a的各二進位全部右移n位,保持符號位不變。右移n位其實就是除以2的n次方

為正數時, 符號位為0,最高位補0

為負數時,符號位為1,最高位是補0或是補1 取決於編譯系統的規定

 

7.學習代碼

 1 #include <stdio.h> 2  3  4 int main() 5 { 6     /* 按位與 & 7       8      10101010000 9      0000010000010      -------------11      0000000000012      13      1011101114      1010110115      ---------16      1010100117      18      100119      010120      -----21      000122      */23     24     /*25      按位或 |26      100127      010128      -----29      110130      */31     32     33     /*34      按位異或 ^ 35      1.相同數值進行異或,結果肯定是0,比如9^936      2.交換 9^5^6 == 9^6^537      3.任何數值跟0進行異或,結果還是原來的數值,9^0 == 938      4.a^b^a == a^a^b == 0^b == b39      40      100141      010142      -----43      110044      45      100146      100147      -----48      0000049      50      010151      000052      ----53      010154      55      9^5^9 == 9^9^5 = 0^5 = 556      57      a^b^a == b58      */59     //printf("%d\n", 9^9);60     61     //printf("%d\n", 9 ^ 5);62     63     /*64      按位取反 ~65      ~0000 0000 0000 0000 0000 0000 0000 100166       1111 1111 1111 1111 1111 1111 1111 011067      */68     //printf("%d\n", ~9);69     70     /*71      左移 <<72      73      0000 0000 0000 0000 0000 0000 0000 000074      00 0000 0000 0000 0000 0000 0000 10010075      76      9<<1 -> 9 * 2的1次方 == 1877      9<<2 -> 9 * 2的2次方 ==3678      9<<n -> 9 * 2的n次方79      */80     81     //printf("%d\n", 9<<1);82     83     /*84      右移 >>85      0000 0000 0000 0000 0000 0000 0000 000086      000000 0000 0000 0000 0000 0000 0000 1087      111111 1111 1111 1111 1111 1111 1111 10 88      89      8>>1 -> 8/2 == 490      8>>2 -> 8/2的2次方 == 291      8>>n -> 8/2的n次方92      */93     94     printf("%d\n", 8>>3);95     96     return 0;97 }
 1 #include <stdio.h> 2  3 /* 4  使用位異或運算子交換兩個變數的值 5  */ 6  7 int main() 8 { 9     int a = 10;10     int b = 11;11     12     /* 藉助第三方變數13     int temp = a;14     a = b;15     b = temp;16     */17     18     /*19     a = b - a;20     b = b - a;21     a = b + a;22     */23     24     // a^b^a == b25     26     // a -->  10^1127     // b -->  1028     a = a ^ b;29     b = a ^ b;30     a = a ^ b;31     32     printf("a=%d, b=%d\n", a, b);33     34     return 0;35 }
 1 #include <stdio.h> 2 /* 3  用位與&運算子判斷變數的奇偶性 4  */ 5 int main() 6 { 7     /* 8      15: 1111 9      9:  100110      11      14: 111012      10: 101013      */14     int a = 15;15     16     a&1 == 1 // 奇數17     a&1 == 0 // 偶數18     19     /*20     if (a%2) {21         printf("奇數\n");22     } else {23         printf("偶數\n");24     }*/25     26     //a%2==0?printf("偶數\n"):printf("奇數\n");27     28     //a%2?printf("奇數\n"):printf("偶數\n");29     30     31     32     return 0;33 }
 1 /* 2 寫一個函數,用來輸出整數在記憶體中的二進位形式 3 */ 4  5 #include <stdio.h> 6 void printBinary(int number); 7  8 int main() 9 {10     /*11      0000 0000 0000 0000 0000 0000 0000 000012      0000 0000 0000 0000 0000 0000 0000 111113      14      9 : 0000 0000 0000 0000 0000 0000 0000 100115      -10 : 1111 1111 1111 1111 1111 1111 1111 011016      */17     18     19     //printf("%d\n", ~9);20     21     22     printBinary(-10);23     return 0;24 }25 26 void printBinary(int number)27 {28     29     // 記錄現在挪到第幾位30     // (sizeof(number)*8) - 1 == 3131     int temp = ( sizeof(number)<<3 ) - 1;32     33     while ( temp >= 0 )34     {35         // 先挪位,再&1,取出對應位的值36         int value = (number>>temp) & 1;37         printf("%d", value);38         39         // 40         temp--;41         42         // 每輸出4位,就輸出一個空格43         if ( (temp + 1) % 4 == 0 )44         {45             printf(" ");46         }47     }48     49     printf("\n");50 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.