当前位置:网站首页>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 .
边栏推荐
- 现在股票开户可以直接在网上开吗?安全吗。
- For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
- 最长公共前缀(leetcode题14)
- 2022年投资哪个理财产品收益高?
- ant desgin 多选
- POJ 1182: food chain (parallel search) [easy to understand]
- 5billion, another master fund was born in Fujian
- Hongmeng smart home [1.0]
- ASP.NET幼儿园连锁管理系统源码
- Numpy——2. Shape of array
猜你喜欢
State mode - Unity (finite state machine)
Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)
PMP每日一练 | 考试不迷路-7.7
PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!
Numpy——2. Shape of array
让这个 CRMEB 单商户微信商城系统火起来,太好用了!
2022.07.05
PMP practice once a day | don't get lost in the exam -7.7
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
转置卷积理论解释(输入输出大小分析)
随机推荐
What does "true" mean
Key points of anti reptile: identifying reptiles
Notes...
歌单11111
LeetCode 648(C#)
浏览积分设置的目的
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
Micro service remote debug, nocalhost + rainbow micro service development second bullet
时间工具类
杰理之关于 TWS 交叉配对的配置【篇】
解决远程rviz报错问题
Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
L1-025 positive integer a+b (Lua)
2022.07.05
L1-027 rental (Lua)
2022.07.02
Business experience in virtual digital human
How many are there (Lua)
“本真”是什么意思