当前位置:网站首页>位运算符讲解
位运算符讲解
2022-07-04 22:31:00 【&volume】
目录
1.>> 右移
2.<<左移
最近接触了位运算符,我认为位运算符在一些题中的使用是很巧妙得,而且它也是算是一个入门算法。
位运算符有,与,或,非,异或,移位运算符
位运算符实际是采取二进制进行运算,
与(&)
两个二进制操作数对应位同为1 结果位 才为1,其余情况为0;
10进制数 15 二进制 00001111
14 00001110
15&14= 14(00001110)
# include <stdio.h>
int main()
{
printf("%d",14&15);
return 0;
}
或(|)
两个二进制操作数对应位只要有一个为1 结果位 就为1,其余情况为0
10进制数 15 二进制 00001111
14 00001110
15|14=15(00001111)
# include <stdio.h>
int main()
{
printf("%d",14|15);
return 0;
}
非(~)
一个二进制操作数,对应位为0,结果位为1;对应位为1,结果位为0;
取反后得到的是原码 而进行存储时是存储的补码,得到补码的过程要先得到反码
10进制数 8 二进制 0000 1000
(~8)=-9(1000 1001)
取反后原码:1111 0111
取反后反码:1000 1000(除了符号位 其余位都进行取反操作)
取反后补码:1000 1001 补码(因为为负数所以加一) 如果为正数不需要进行加一
# include <stdio.h>
int main()
{
printf("%d",~15);
return 0;
//00001000
//11110111 取反原码
//10001000 取反反码
//10001001 补码(因为为负数所以加一)
}
异或(^)
同为假,异为真
10进制数 15 二进制 00001111
14 00001110
15^14=1(0000001)
# include <stdio.h>
int main()
{
printf("%d",14^15);
return 0;
}
移位运算符
1.>> 右移
按二进制形式把所有的数字向右移动对应位移位数,低位移出(舍弃),高位的空位补符号位,即正数补零,负数补1。(高位用符号位补充-------正数右移还是正数 负数右移还是负数)
语法:需要移位的数字number >> 移位的次数
13>>2=3
13的二进制位(1101)向右移动两位变成3(11)
性质(在整数下):右移相当于用该数除以2的移位次数再向下取整
13/(2^2)向下取整为3
2.<<左移
将一个数的二进制同时往左移位,低位用0补充
5<<1=10
5(101)向右移动1位 (1010)=10、
性质同上右移
是5*2^1=10
边栏推荐
- 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
- MySQL Architecture - logical architecture
- 攻防世界 MISC 进阶区 hit-the-core
- Unity-VScode-Emmylua配置报错解决
- 【室友用一局王者荣耀的时间学会了用BI报表数据处理】
- 繁华落尽、物是人非:个人站长该何去何从
- 攻防世界 MISC 高手进阶区 001 normal_png
- Redis入門完整教程:Pipeline
- Analysis of the self increasing and self decreasing of C language function parameters
- The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
猜你喜欢
Redis入门完整教程:HyperLogLog
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Talk about Middleware
攻防世界 misc 高手进阶区 a_good_idea
Install the gold warehouse database of NPC
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
Redis入门完整教程:客户端通信协议
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
A complete tutorial for getting started with redis: transactions and Lua
Attack and defense world misc advanced area ditf
随机推荐
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
MySQL Architecture - user rights and management
关于栈区、堆区、全局区、文字常量区、程序代码区
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
特征缩放 标准化 归一化
MySQL Architecture - logical architecture
String类中的常用方法
Redis入门完整教程:键管理
Duplicate ADMAS part name
浅聊一下中间件
【OpenGL】笔记二十九、抗锯齿(MSAA)
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
Introducing QA into the software development lifecycle is the best practice that engineers should follow
Advanced area a of attack and defense world misc Masters_ good_ idea
Logo special training camp section II collocation relationship between words and graphics
Talk about Middleware
Redis入门完整教程:有序集合详解
Detailed explanation of heap sort code
A complete tutorial for getting started with redis: Pipeline
Advanced area of attack and defense world misc 3-11