当前位置:网站首页>位运算介绍
位运算介绍
2022-07-07 17:26:00 【华灯初上`】
从雨巨那里学的位运算,来分享一波
什么是位运算?
位运算:位运算是系统所提供的直接对数字的数位进行操作的运算。具体有以下几种操作:
<< 左移运算符
>> 右移运算符
| 或
& 与
~ 取反
^ 异或
系统存储数字是按照二进制存储的,即内存中的每个数字都是由0、1构成的字符串;引入十进制中的进制词(即个、十、百位等,注意,这里只是引用,所描述的对象依然是二进制的0、1字符串),位运算就是直接对处于某些进制上的数字进行改变,举几个例子说明一下:
以下括号中字符串均为二进制,不再予以标注
- (1010)>>1 此操作的意思就是1010右移一位,结果变成了(101);因为第四位的0由于右移被舍去,如果(1010)>>2,结果就是(10),因为最后的10均被舍去。
- (1010)<<1 此操作的意思就是1010左移一位,结果是(10100);多出来的位数补0,十位上的1移到了百位上。
- & 、 | 操作与编程语言中的 || 和 && 操作相同,若都为1则&的结果为1,有一者为1则 | 的结果为1,其余情况结果都为0。如1010 & 0010=0010,1010 | 0010=1010。
- ~ 操作不同于编程语言中的!,此操作是令二进制字符串中的0、1互换,即0变成1,1变成0;如 ~1010 结果为 0101 。
- ^ 异或操作,就是判断两者是不是不一样,不一样的话就是1,一样的话就是0。比如1010 ^ 0011 = 1001。
- 其中,进行异或运算时,任何数异或0不变,任何数异或1就取反。如1010 ^ 1=1011。
- 这里要注意的是,&、|、^操作均为两字符串相对应位置的单个字母的操作,例如:1010 | 1=1011,后者只有一位,至于前者的最后一位进行或运算,前者的前三位不受影响。
位运算的小技巧:
介绍一个位运算的小技巧:
a=a^b;
b=a^b;
a=a^b;
问:这三个语句有什么作用?
我们推导一下便知:
可见,语句的作用是交换a,b的值。
位运算的基础操作:
基础的位运算操作很重要,这里列举部分:
- 去掉最后一位:x>>1
- 在最后加一个0:x<<1
- 在最后加一个1:x<<1 + 1(+也可用 | )
- 把最后一位变成1:x | 1
- 把最后一位变成0:(x>>1)<<1 或者 x | 1 - 1
- 最后一位取反:x^1
- 把右数第k位变成1:x | (1<<(k-1))
- 把右数第k位变成0:x & (~(1<<(k-1)))
运算优先级:
在写代码的时候一定要注意运算符的优先级不同可能造成的影响,所以一定要加括号。
边栏推荐
- Short selling, overprinting and stock keeping, Oriental selection actually sold 2.66 million books in Tiktok in one month
- what‘s the meaning of inference
- Realize payment function in applet
- Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
- how to prove compiler‘s correctness
- 歌单11111
- Kirin Xin'an cloud platform is newly upgraded!
- POJ 2392 Space Elevator
- [information security laws and regulations] review
- Uvalive – 4621 CAV greed + analysis "suggestions collection"
猜你喜欢
Research and practice of super-resolution technology in the field of real-time audio and video
5billion, another master fund was born in Fujian
[tpm2.0 principle and Application guide] Chapter 16, 17 and 18
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
Jerry's headphones with the same channel are not allowed to pair [article]
Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
[Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question
648. 单词替换
Command mode - unity
随机推荐
2022上半年朋友圈都在传的10本书,找到了
Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
[mime notes]
SlashData开发者工具榜首等你而定!!!
POJ 1182: food chain (parallel search) [easy to understand]
博睿数据入选《2022爱分析 · IT运维厂商全景报告》
Do you know all four common cache modes?
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
Key points of anti reptile: identifying reptiles
Big Ben (Lua)
脑洞从何而来?加州大学最新研究:有创造力的人神经连接会「抄近道」
5billion, another master fund was born in Fujian
[Base64 notes] [suggestions collection]
基于图像和激光的多模态点云融合与视觉定位
Research and practice of super-resolution technology in the field of real-time audio and video
2022.07.04
最长公共前缀(leetcode题14)
“本真”是什么意思
【Base64笔记】「建议收藏」