当前位置:网站首页>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 ~!
边栏推荐
- 英文翻译意大利语-批量英文翻译意大利语工具免费
- 【微信小程序开发】页面导航与传参
- Germany and Portugal have announced that they will not disable Huawei 5g equipment, but Germany will set strict restrictions!
- 基于QTGUI图像界面的空战游戏设计
- 企业级分布式爬虫框架入门
- [notes] Apocalypse: list of practical experience and reflection of product managers
- SaltStack之数据系统
- Dockler的基础用法
- [深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
- The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
猜你喜欢

Business visualization - let your flowchart "run" (4. Actual business scenario test)

你知道雨的类型有几种?

OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)

Sword finger offer II 109. unlock the password lock

Saltstack configuration management

WPF 实现带蒙版的 MessageBox 消息提示框

VAE: understanding and implementation of variational self encoder

Rust 入门指南(rustup, cargo)

毕马威中国:证券基金经营机构信息技术审计项目发现洞察

Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters
随机推荐
投资35.45亿元!格力集团参与小米产业基金
source insight项目导入和使用教程
After reading the thesis for three years, I learned to read the abstract today
这个客制化键盘,秀翻我了~
[深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
初步学习函数(第3篇博客)
Quickly install torch spark, torch geometric and other packages in moment pool cloud
Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022
[notes] Networking: Internet product managers change the world
This customized keyboard turns me on~
Cell综述:人类微生物组研究中的单细胞方法
Dockler的基础用法
Test Development Notes
为什么在telnet登入界面下没有日志输出?
Convertible bond concept table x notation gives you a convenient and fast experience!
WPF implements MessageBox message prompt box with mask
MySQL performance testing tool sysbench learning
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
Prometheus部署
远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
