#include <stdio.h>
int main(){
/*
1.按位与运算符:&
1 & 0 = 0;
0 & 1 = 0;
0 & 0 = 0;
1 & 1 = 1;
0001 = 1;0010 = 2;
0011 = 3;0100 = 4;
0101 = 5;0110 = 6;
0111 = 7;1000 = 8;
1001 = 9;1010 = 10;
1011 = 11;1100 = 12;
1101 = 13;1110 = 14;
1111 = 15;0001 0000 = 16;
*/
/*
22:0001 0110
&
7:0000 0111
--------------
6 0000 0110
*/
/*
-22:1110 1010(取反加1)
&
7:0000 0111
----------------
2:0000 0010
*/
int a1 = 22 & 7;
int a2 = -22 & 7;
printf("a1 = %d, a2 = %d\n",a1,a2); //>>>a1 = 6, a2 = 2
//-----------------------------------------------------------------------------------------------------------------------------------
/*
按位或运算:|
1 & 0 = 1;
0 & 1 = 1;
0 & 0 = 0;
1 & 1 = 1;
*/
/*
22:0001 0110
|
7:0000 0111
--------------
6 0001 0111
*/
int b1 = 22 | 7;
printf("b1 = %d\n",b1);//>>>b1 = 23
//--------------------------------------------------------------------------------------------------------------------
/*
按位取反运算:~(一元运算符)
公式:~n = -(n+1)
以下例子基于8位计算机:
~1 = -2;
1: 0000 0001
~1:1111 1110(从此处可见该数为负数)
已知二进制位要求原始数据:取反加1
0000 0001-->0000 0010
--------------------------------------------
~0 = -1;
0000 0000
1111 1111(~0:从此处可观察该数为负数)
0000 00001;
-------------------------------------------------
负数补码的求法:
确定符号位为1,写出其原码:1000 0010(-2)
符号位不变,写出其绝对值的原码:1111 1101
对原码各位取反加1:1000 0011(负数在计算机中以补码的方式存储)
~-2= 3(8位计算机)
-2:1000 0011
~-2:0111 1100(可见为正数)
已知二进制位要求原始数据:取反加1
0000 0011
0111 1101(~-2:从此处可见概述为正数)
1000 0011(取反加1)
*/
int c1 = ~1;
int c2 = ~0;
int c3 = ~-2;
printf("c1 = %d, c2 = %d, c3 = %d\n",c1,c2,c3);
return 0;
}
位运算(未完成)
最新推荐文章于 2022-07-29 15:05:52 发布