当前位置:网站首页>Introduction to bit operation
Introduction to bit operation
2022-07-07 19:40:00 【The lights are on`】
Bit operation learned from Yu Ju , To share a wave of
What is bit operation ?
An operation : Bit operation is the operation provided by the system to directly operate the digits of numbers . There are the following operations :
<< Shift left operator
>> Shift right operator
| or
& And
~ Take the opposite
^ Exclusive or
The system stores numbers in binary , That is, every number in memory is composed of 0、1 The string formed ; Introduce the decimal word ( I.e 、 Ten 、 One hundred, etc , Be careful , This is just a quote , The object described is still binary 0、1 character string ), Bit operation is to directly change the number in some base , Give a few examples to illustrate :
The strings in the following brackets are binary , No more marking
- (1010)>>1 This operation means 1010 Moves to the right one , It turned out to be (101); Because the fourth 0 Due to the right shift, it is discarded , If (1010)>>2, The result is (10), Because the last 10 Are all rounded off .
- (1010)<<1 This operation means 1010 The left one , The result is (10100); The extra digits make up 0, Top ten 1 Moved to the hundreds .
- & 、 | Operation and programming language || and && Same operation , If all are 1 be & As the result of the 1, One is 1 be | As the result of the 1, In other cases, the result is 0. Such as 1010 & 0010=0010,1010 | 0010=1010.
- ~ The operation is different from !, This operation is to make 0、1 swap , namely 0 become 1,1 become 0; Such as ~1010 The result is 0101 .
- ^ Exclusive or operation , Is to judge whether the two are different , The difference is 1, The same thing is 0. such as 1010 ^ 0011 = 1001.
- among , In XOR operation , Any number exclusive or 0 unchanged , Any number exclusive or 1 It's the opposite . Such as 1010 ^ 1=1011.
- What we should pay attention to here is ,&、|、^ The operation is the operation of a single letter in the corresponding position of two strings , for example :1010 | 1=1011, The latter has only one , As for the last bit of the former, or operation , The first three of the former are not affected .
Bit operation tips :
Introduce a bit operation tips :
a=a^b;
b=a^b;
a=a^b;
ask : What are the functions of these three statements ?
Let's deduce it to know :
so , The function of a statement is to exchange a,b Value .
Basic operation of bit operation :
Basic bit operations are important , Here are some parts of :
- Get rid of the last one :x>>1
- Add one at the end 0:x<<1
- Add one at the end 1:x<<1 + 1(+ Also available | )
- Turn the last one into 1:x | 1
- Turn the last one into 0:(x>>1)<<1 perhaps x | 1 - 1
- Last negative :x^1
- Number right k A into 1:x | (1<<(k-1))
- Number right k A into 0:x & (~(1<<(k-1)))
Operation priority :
When writing code, you must pay attention to the possible impact of different operator priorities , So be sure to add parentheses .
边栏推荐
- 2022.07.05
- [RT thread env tool installation]
- State mode - Unity (finite state machine)
- R语言dplyr包mutate_at函数和min_rank函数计算dataframe中指定数据列的排序序号值、名次值、将最大值的rank值赋值为1
- Numpy——2.数组的形状
- Uvalive – 4621 CAV greed + analysis "suggestions collection"
- Ucloud is a basic cloud computing service provider
- 小试牛刀之NunJucks模板引擎
- 项目经理『面试八问』,看了等于会了
- [HDU] 5248 sequence transformation (greedy + dichotomy) [recommended collection]
猜你喜欢
Research and practice of super-resolution technology in the field of real-time audio and video
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
[RT thread env tool installation]
Business experience in virtual digital human
【RT-Thread env 工具安装】
2022.07.05
AD域组策略管理
杰理之手动配对方式【篇】
Jerry's headphones with the same channel are not allowed to pair [article]
CMD command enters MySQL times service name or command error (fool teaching)
随机推荐
Uvalive – 4621 CAV greed + analysis "suggestions collection"
Kirin Xin'an joins Ningxia commercial cipher Association
Solve the problem of remote rviz error reporting
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
杰理之测试盒配置声道【篇】
First time in China! The language AI strength of this Chinese enterprise is recognized as No.2 in the world! Second only to Google
Specify the version of OpenCV non-standard installation
网信办公布《数据出境安全评估办法》,9 月 1 日起施行
鸿蒙智能家居【1.0】
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
杰理之相同声道的耳机不允许配对【篇】
ant desgin 多选
2022.07.02
How to open an account for stock speculation? Excuse me, is it safe to open a stock account by mobile phone?
多个kubernetes集群如何实现共享同一个存储
杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
编译原理 实验一:词法分析器的自动实现(Lex词法分析)
LeetCode 648(C#)
R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
LeetCode1051(C#)