当前位置:网站首页>位运算介绍
位运算介绍
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)))
运算优先级:
在写代码的时候一定要注意运算符的优先级不同可能造成的影响,所以一定要加括号。

边栏推荐
- Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
- 99% of people don't know that privatized deployment is also a permanently free instant messaging software!
- For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
- Do you know all four common cache modes?
- testing and SQA_动态白盒測试[通俗易懂]
- Golang client server login
- In the first half of 2022, I found 10 books that have been passed around by my circle of friends
- LeetCode1051(C#)
- Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
- 2022.07.05
猜你喜欢
![[tpm2.0 principle and Application guide] Chapter 16, 17 and 18](/img/7a/b16549590e6445d9199c8000d47d36.png)
[tpm2.0 principle and Application guide] Chapter 16, 17 and 18

AD域组策略管理

99% of people don't know that privatized deployment is also a permanently free instant messaging software!

Research and practice of super-resolution technology in the field of real-time audio and video

Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry

Former richest man, addicted to farming

ES6 note 1

2022.07.02

前首富,沉迷种田

Responsibility chain model - unity
随机推荐
杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
L1-025 positive integer a+b (Lua)
L1-019 who falls first (Lua)
Classification and application of enterprise MES Manufacturing Execution System
LeetCode 535(C#)
Unable to link the remote redis server (solution 100%
谷歌seo外链Backlinks研究工具推荐
AI写首诗
我的创作纪念日
ES6 note 1
how to prove compiler‘s correctness
Redis master-slave and sentinel master-slave switchover are built step by step
Matplotlib drawing 3D graphics
杰理之关于 TWS 声道配置【篇】
Do you know all four common cache modes?
Command mode - unity
In the first half of 2022, I found 10 books that have been passed around by my circle of friends
索引总结(突击版本)
指定opencv非标准安装的版本