c 语言 位运算笔记。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 //&  
 5 // 11 1
 6 // 10 0
 7 // 01 0
 8 // 00 0
 9 
10 //1001 0011   
11 //1111 0000
12 //1001 0000
13 
14 //10 -》0 00-》0   1,0 0-》0
15 //11 -》1  0&1=0   1,0与1想与,结果不变
16 
17 //XXXX XXXX
18 //1111 0111  
19 
20 //1011 0111 
21 //1111 0000
22 //1011 0000
23 //>>4
24 
25 //0000 1111
26 //0000 0111
27 //  X   Y
28 
29 
30 void main1()
31 {
32     unsigned char ch = 255;// 1111 1111
33     unsigned char nch = 15;// 0000 1111
34                            // 0000 1111
35     unsigned char rch = ch&nch;
36     printf("%d", rch);
37 
38 
39     getchar();
40 
41 
42 }

位或:

 1 #include<stdio.h>
 2 #include <stdlib.h>
 3 
 4 // |
 5 // 11 1
 6 // 10 1
 7 // 01 1
 8 // 00 0
 9 
10 //XXXX  XXXX
11 //0000  1111
12 
13 //11 ->1 01-> 1  0,1 -1 ->1
14 //10->1  00->0   1,0  -0 不变
15 
16 
17 
18 
19 void main2()
20 {
21 
22     unsigned char ch = 255;// 1111 1111
23     unsigned char nch = 15;// 0000 1111
24                            // 1111 1111
25     unsigned char rch = ch|nch;
26     printf("%d", rch);
27 
28 
29     getchar();
30 
31 }

异或:

#include <stdio.h>
#include <stdlib.h>

// ^
// 11 0


// 00 0
// 10 1


// 01 1

//10 ->1  ,00->0   0 不变
//01->1  ,11->0  遇到1反转

//1011 0111
//0000 1111
//1011 1000


void main6()
{
    int a = 10; // 0000 1010
     
    int b = 20;//  0001 0100

    printf("\na=%d,b=%d", a, b);
    a = a^b;   // 0000 1010 a

              //  0001 0100 b
               // 0001 1110 a
    b = a^b;  //  0000 1010 b=10
               // 0001 1110 a
    a = a^b;   // 0001 0100 a=20


    printf("\na=%d,b=%d", a,b);

    system("pause");





}

void  main4()
{
    unsigned char ch = 255;// 1111 1111
    unsigned char nch = 0;

    unsigned char rch = ch^nch;
    printf("%d", rch);




    system("pause");



}


void main3()
{
    unsigned char ch = 255;// 1111 1111
    unsigned char nch = 15;// 0000 1111
                           // 1111 0000
    unsigned char rch = ch^nch;
    printf("%d", rch);




    system("pause");

}

 

    原文作者:宿森
    原文地址: http://www.cnblogs.com/susen/p/5314530.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞