当前位置:网站首页>Thoroughly understand bit operations - shift left, shift right
Thoroughly understand bit operations - shift left, shift right
2022-07-28 19:40:00 【Programmer Li ha】
I believe you can see binary operations in various languages and frameworks . Move left 、 Move right 、&、|、^ Operation, etc. . Then this post makes you thoroughly understand shift left 、 Move right .
First of all, we should distinguish the left shift 、 That's right shift , It's very simple , Distinguish from the direction pointed by the arrow .<< Move left ,>> Move right
Move left :
To put it simply, put the current binary , Move the whole to the left N A unit of ,N Depends on your expression .
Then take an example , And draw pictures to understand .

hold 32 Binary system ( The blue part ) Move the whole one to the left . Get the picture below .

0100 0000 Binary to decimal 64. So the answer is 64.
Then another topic , Let's make a small change with the previous topic .
32 << 2 = 128, The answer for 64. Now let's use drawing to understand .

hold 32 Binary part of ( The blue part ) Move to the left 2 Units get the following figure

Binary system 1000 0000 Convert to decimal to 128.
So every left shift operation is so troublesome , We have to convert the decimal system into binary , After moving , Then convert from binary to decimal , It's too much trouble . So we need to find the rules .
The law is : Source operands * 2 Of N Power (N Depends on the number of bits moved ) = The result of the move .
32 << 2 = 32 * 2^2 = 128
16 << 4 = 16 * 2^4 = 256
7 << 2 = 7 * 2^2 = 28
Move right :
Simply put , Put the current binary , Move the whole to the right N The unit of , Get a new binary .
Let's use an example + Draw pictures to understand .
32 >> 1 = 16.32 Moves to the right one , The answer for 16. Now let's draw a picture to understand .

hold 0010 0000 The whole blue part moves to the right , This was 6 To represent a binary , Turned into 5 To represent a binary , Another one has been moved to the right .
So get 0001 0000 Convert it to decimal 16.
12 >> 4 = 0.12 Shift four places to the right , The answer for 0. Now let's draw a picture to understand .
hold 0000 1100 Move right 4 position , Just put the last 4 The position has been moved out , Just at the end 4 position 1100 Expressed as 12. Move right to get 0000 0000 So the final answer is 0.
Then let's find the rules
The law is : Source operands / 2 Of N Power (N Depends on the number of bits moved ) = The result of the move ( Only integer part )
32 >> 2 = 32 / 2^2 = 8
17 >> 2 = 17 / 2^2 = 4.25 = 4( Because only the integer part is taken )
512 >> 10 = 512 / 2^10 = 0.5 = 0( Because only the integer part is taken )
Why is bit operation efficient
Originally, I wanted to compare with intel Develop manual level to find information to demonstrate the efficiency of bit operation . But in assembly, shifting left and right is an instruction , Like addition, subtraction, multiplication and division, it is an instruction . The corresponding instruction is intel The explanation in the development manual does not mention efficiency , Just tell developers how to use these instructions ( It can also be said that the author's ability is poor , Can't find ).
But from a macro perspective , Whether it's left or right , We all know the rules from the above , If you don't use shift left and shift right , Their calculation steps are divided into several steps , And an operation is CPU An instruction execution cycle of , Moving left and right only requires moving N It's binary , In this way, there will be several more instruction execution cycles than using shift left and shift right directly , Maybe when there are few operations , Little difference . But the advantage of using bit operation in large-scale operation appears .
Everyone should understand , Both system development and hardware development use C And compilation , Where is their high efficiency ? A lot of advanced packaging is missing , Reduce the number of compilations , Basically for CPU Instruction set programming . And bit operations can also be analogized in this way . For the bottom of the computer, it is composed of binary , Binary computing is more inclined to low-level Computing .
summary
Last , If this post is helpful to you , Hope you can praise it + Focus on + Collection ! Your support is my greatest motivation , The use of various frameworks and the source code interpretation of the framework will be updated all the time ~!
边栏推荐
- 英文翻译意大利语-批量英文翻译意大利语工具免费
- leetcode day3 超过经理收入的员工
- How does app automated testing achieve H5 testing
- 芯片功耗性能验证:从困境到超越
- Redis笔记
- 开盘暴涨215%!国产信号链芯片企业芯海科技登陆科创板
- Serial port receiving application ring buffer
- 【笔记】《结网:互联网产品经理改变世界》
- shared_ptr 和 make_shared 的使用
- English article translation - English article translation software - free batch translation
猜你喜欢

业务可视化-让你的流程图“Run“起来(4.实际业务场景测试)

NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!

基于QTGUI图像界面的空战游戏设计

企业级分布式爬虫框架入门

Smart contract security - overflow vulnerability

Pagoda panel construction novel CMS management system source code measurement - thinkphp6.0

Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation

JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover

Pytoch: implementation of crossentropyloss and labelsmoothing

JS preventDefault() 键盘输入限制 onmousewheel stopPropagation停止事件传播
随机推荐
CodeIgnier框架实现restful API接口编程
Test Development Notes
Redis master-slave architecture (how to calculate sizeof function)
在矩池云快速安装torch-sparse、torch-geometric等包
并发程序设计,你真的懂吗?
First blog
Design of library management database system
MySQL8 基于clone创建主从复制
OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)
[深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
Left alignment function of Lua language (handwriting)
英文翻译西班牙语-批量英文翻译西班牙工具免费
MySQL8 Encrypting InnoDB Tablespaces
Prometheus部署
lua语言的左对齐函数(手写)
Investment of 3.545 billion yuan! Gree Group participates in Xiaomi industry fund
中国首枚芯片邮票面世:内置120um超薄NFC芯片
业务可视化-让你的流程图“Run“起来(4.实际业务场景测试)
架构基本概念和架构本质
【微信小程序开发】页面导航与传参
