当前位置:网站首页>位运算符讲解
位运算符讲解
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
边栏推荐
- SQL中MAX与GREATEST的区别
- 攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
- MYSQL架构——逻辑架构
- Microservices -- Opening
- Why is Dameng data called the "first share" of domestic databases?
- 特征缩放 标准化 归一化
- Logo special training camp Section IV importance of font design
- Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
- 常用技术指标之一文读懂BOLL布林线指标
- Introducing QA into the software development lifecycle is the best practice that engineers should follow
猜你喜欢

Redis入门完整教程:客户端通信协议

Install the gold warehouse database of NPC

A complete tutorial for getting started with redis: redis shell

The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development

Close system call analysis - Performance Optimization

Analysis of the self increasing and self decreasing of C language function parameters

Attack and defense world misc advanced grace-50

Redis入门完整教程:Redis Shell

共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf

攻防世界 MISC 进阶 glance-50
随机推荐
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
常用技术指标之一文读懂BOLL布林线指标
Persistence mechanism of redis
Three stage operations in the attack and defense drill of the blue team
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
MYSQL架构——用户权限与管理
特征缩放 标准化 归一化
Redis入门完整教程:键管理
安装人大金仓数据库
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
攻防世界 MISC 进阶区 hong
Sword finger offer 68 - I. nearest common ancestor of binary search tree
串口数据帧
Breakpoint debugging under vs2019 c release
Redis入门完整教程:Redis使用场景
质量体系建设之路的分分合合
How to manage 15million employees easily?
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
Tla+ introductory tutorial (1): introduction to formal methods
【OpenGL】笔记二十九、抗锯齿(MSAA)