当前位置:网站首页>位运算符讲解
位运算符讲解
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
边栏推荐
- 醒悟的日子,我是怎么一步一步走向软件测试的道路
- SPSS installation and activation tutorial (including network disk link)
- MySQL Architecture - logical architecture
- Mongodb aggregation operation summary
- 图片懒加载的原理
- 攻防世界 misc 进阶区 2017_Dating_in_Singapore
- 通过Go语言创建CA与签发证书
- 攻防世界 MISC 进阶区 3-11
- Wake up day, how do I step by step towards the road of software testing
- Redis入门完整教程:发布订阅
猜你喜欢
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
Redis: redis configuration file related configuration and redis persistence
A complete tutorial for getting started with redis: hyperloglog
安装人大金仓数据库
Redis入门完整教程:哈希说明
Attack and defense world misc master advanced zone 001 normal_ png
Redis入门完整教程:慢查询分析
Complete tutorial for getting started with redis: bitmaps
攻防世界 MISC 进阶 glance-50
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
随机推荐
Microservices -- Opening
企业如何跨越数字化鸿沟?尽在云原生2.0
浅聊一下中间件
Redis入门完整教程:Redis使用场景
Redis入门完整教程:键管理
Close system call analysis - Performance Optimization
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
Redis introduction complete tutorial: slow query analysis
9 - class
Feature scaling normalization
About stack area, heap area, global area, text constant area and program code area
A complete tutorial for getting started with redis: Pipeline
Why is Dameng data called the "first share" of domestic databases?
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
该如何去选择证券公司,手机上开户安不安全
A complete tutorial for getting started with redis: redis shell
UML图记忆技巧
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
攻防世界 MISC 进阶区 hit-the-core
Hit the core in the advanced area of misc in the attack and defense world