当前位置:网站首页>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 .

边栏推荐
- How to buy stocks on your mobile phone and open an account? Is it safe to open an account
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- How many are there (Lua)
- what‘s the meaning of inference
- 9 原子操作类之18罗汉增强
- R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
- 【Confluence】JVM内存调整
- LeetCode 648(C#)
- PMP practice once a day | don't get lost in the exam -7.7
- CMD command enters MySQL times service name or command error (fool teaching)
猜你喜欢

杰理之手动配对方式【篇】

Policy mode - unity

爬虫实战(七):爬王者英雄图片

关于ssh登录时卡顿30s左右的问题调试处理

杰理之相同声道的耳机不允许配对【篇】

Redis master-slave and sentinel master-slave switchover are built step by step

Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers

PMP每日一练 | 考试不迷路-7.7

J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar

PV static creation and dynamic creation
随机推荐
Kirin Xin'an cloud platform is newly upgraded!
Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog
How to buy bank financial products? Do you need a bank card?
注解。。。
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
IP 工具类
R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
5billion, another master fund was born in Fujian
解决远程rviz报错问题
杰理之关于 TWS 配对方式配置【篇】
How to open an account for stock speculation? Excuse me, is it safe to open a stock account by mobile phone?
L1-028 judging prime number (Lua)
Responsibility chain model - unity
Big Ben (Lua)
R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
一张图深入的理解FP/FN/Precision/Recall
项目经理『面试八问』,看了等于会了
关于ssh登录时卡顿30s左右的问题调试处理