当前位置:网站首页>Basic syntax - bit operation
Basic syntax - bit operation
2022-06-29 07:54:00 【qazw9600】
Displacement
- The data stored in memory moves left and right according to binary bits .
- Move right ( Binary data moves to the right ) The operator :>>
- Move left ( Binary data moves to the left ) The operator :<<
- Examples of use
int a = 1;
int b = a << 1; // Move left 1 position
int c = a >> 2; // Move right 2 position
C Language processing standards
- Move left : Shift and right padding
- Move right : There are two ways to do this
- Unsigned numbers are logically shifted right : Shift and left padding
- Signed numbers are arithmetically shifted right : Shift and left complement sign bit ( Positive complement 0, Negative numbers make up 1).
- Example :
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);
* Running results :
fffffffe
fffffffe
-----------------
7fffffff
ffffffff
Why is there only one processing method for shift left , The right shift is divided into logical right shift and arithmetic right shift
- Because left shift is often used in multiplication ( Moving one bit to the left is equivalent to multiplying by 2), Shift right is often used in division operations ( Moving one bit to the right is equivalent to dividing by 2).
- Move left ( multiply 2 After multiple of ) If the data overflows to the sign bit ( The first one on the far left ), Neither signed number nor unsigned number can be represented , Data overflow cannot be avoided , So the left shift is handled uniformly , Moving right does not cause data overflow , To ensure the right shift ( Divide 2) The correctness of the arithmetic result of , You cannot change the sign of a signed number , for example : Signed number -8 Move right 1 Bit divided by 2 The correct answer should be -4, Therefore, the processing method for signed numbers is called arithmetic shift , Unsigned numbers have no sign , Just fill in the zero by logic , So it's called logical shift right .
边栏推荐
猜你喜欢

呕心沥血总结出来的MySQL常见错误以及解决方法(一)

打包时提示: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘

How to permanently set Mysql to utf8 encoding?

How to share the virtual environment of pycharm to jupyter Lab

产品经理应该学习墨刀还是Axure?

Common MySQL errors and solutions summarized painstakingly (I)

手把手系列---安装SpotBugs、并快速上手使用

【工控老马】基于西门子S7-200PLC的跑马灯控制系统的设计方案详解

基于Sophus的Ceres优化

Vulnhub's DC8 target
随机推荐
tf. compat. v1.global_ variables
Electric check code configuration
JSP learning part
程序调试 - Debug/Release 版本
Vibration signal generation and processing based on MATLAB Doppler effect
[industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC
SQL Server 2008 publish and subscribe to SQL Server 2017 pit avoidance Guide
反思 - 中小公司项目管理思维 - 先将产品做出来,先将功能做出来
Kingbasees v8r6 cluster maintenance case - data migration between clusters
C实战——高配版贪吃蛇游戏设计
ROS当中的仿真时间以及Bag包操作
DataTables screen error Popup
进程通信 - 管道
指针引用数组元素
PostgreSQL安装:The database cluster initialisation failed,stack Hbulider安装
路由详解(九阳真经)
code::blocks代码格式化快捷键
【工控老马】PLC六路抢答器系统设计详解
Oracle 批量插入数据-插入民族数据
358. K 距离间隔重排字符串 排序