当前位置:网站首页>基础语法 - 位运算
基础语法 - 位运算
2022-06-29 06:41:00 【qazw9600】
位移
- 内存中存储的数据按二进制位左右移动。
- 右移 (二进制数据向右移动) 操作符:>>
- 左移 (二进制数据向左移动) 操作符:<<
- 使用示例
int a = 1;
int b = a << 1; //左移1位
int c = a >> 2; //右移2位
C语言处理标准
- 左移:移位和右边补零
- 右移:有两种处理方式
- 无符号数采用逻辑右移:移位和左边补零
- 有符号数采用算术右移:移位和左边补符号位(正数补0,负数补1)。
- 示例:
unsigned int a = 0xFFFFFFFF;
int b = 0xFFFFFFFF;
printf("%x\n", a << 1);
printf("%x\n", b << 1);
printf("-----------------\n");
printf("%x\n", a >> 1);
printf("%x\n", b >> 1);
* 运行结果:
fffffffe
fffffffe
-----------------
7fffffff
ffffffff
为何左移只有一种处理方式,而右移却分为逻辑右移和算术右移
- 因为左移常用于乘法运算中(左移一位相当于乘以2),右移常用于除法运算中(右移一位相当于除以2)。
- 左移(乘以2的倍数后)如果数据溢出到符号位(最左边的第一位),不管是有符号数还是无符号都无法进行体现,数据溢出无法规避,所以左移都统一处理,而右移不会导致数据溢出,为了保证右移(除以2)的算数结果的正确性,不能改变有符号数的正负性,例如:有符号数-8右移1位除以2正确答案应该是-4,所以有符号数采用的处理方法叫做算术位移,而无符号数没有符号,按逻辑补零就好,所以叫做逻辑右移。
边栏推荐
- Prompt during packaging: property 'sqlsessionfactory' or 'sqlsessiontemplate'‘
- Appium environment setup
- 【工控老马】基于PLC的花样喷泉设计原理详解
- 【工控老马】PLC六路抢答器系统设计详解
- DataTables screen error Popup
- 施努卡:什么是视觉定位系统 视觉定位系统的工作原理
- 数组知识点小结
- How to solve the cross domain problem of mobile phone accessing the web in the web development scenario
- pycharm的虚拟环境如何共享到jupyter-lab
- Simulation analysis of sailing dynamics
猜你喜欢
随机推荐
搭建jenkins环境并自动关联打包好的工程jar进行自动发布
【工控老马】西门子PLC s7-300SCL编程详解
关于开发web场景下如何解决手机访问web跨域问题
Up and down transitions in polymorphism
How to permanently set Mysql to utf8 encoding?
SQL injection bypass (6)
施努卡:轮胎自动抓取安装,3D视觉定位,机器人自动抓取
Appium automation test foundation ADB common commands (III)
Check whether tensorflow supports GPU and test program
ES中配置ext.dic文件不生效的原因
Appium automation test foundation ADB common commands (II)
Appium自动化测试基础 — ADB常用命令(三)
【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结
Compiling principle: the king's way
Vulnhub's dc6 target
Common MySQL errors and solutions summarized painstakingly (I)
游标长时间open导致表无法vacuum问题
Concurrent idempotent anti shake
Oracle batch insert data - insert ethnic data
数组知识点小结









