当前位置:网站首页>Deep understanding of bit operations
Deep understanding of bit operations
2022-06-27 13:36:00 【Classmate an downstairs】

What is base
The decimal system is also called Carry counting system , It's a human defined count method with carry ( There are counting methods without carry , For example, the original twine counting method , Commonly used in singing “ just ” Word count , And similar tally mark Count ). For any system —X Base number , It means that the number operation on each bit is every X Into one . The decimal system is one in ten , Hexadecimal is every hexadecimal into one , Binary system is a binary system , And so on ,x The base is the square x carry .
So What is the essence of operation Well ? I think it's counting . Break the inherent thought , We think of every number as Symbol , This is much more interesting , We can write our own radix , You can do your own calculations
Hexadecimal conversion
1、 Other decimal system to decimal system
Let's start at the lowest point , Extract the number in each bit , Multiplied by the base number ( digit -1) Power , Sum up
// 2^n Binary for :1{n individual 0} 2^n-1 Binary for :{n individual 1}
Binary to decimal : 1011 = 1*2^0 + 1*2^1 + 0*2^2 + 1*2^3 = 11
Octal to decimal : 0123 = 3*8^0 + 2*8^1 + 1*8^2 = 83
Hexadecimal to decimal : 0x34A = 10*16^0 + 4*16^1 + 3*16^2 = 786
2、 Decimal to other bases
Divide the number by the base number to convert , Know Shang Wei 0 until , Then reverse the remainder
Decimal to binary : 56 = 2^5 + 2^4 + 2^3 = 111000
Decimal to octal : 156 = 0234
Decimal to hexadecimal : 256 = 0x164
3、 Binary to other bases
Group binary numbers into three bits ( Start at the bottom ), Convert to corresponding octal In hexadecimal, there are four digits
Binary to octal : 11 010 101 = 0325
Binary to hexadecimal : 1101 0101 = 0xD5
4、 Other base to binary
Put octal / Each hexadecimal bit is converted to a corresponding one 3/4 A binary number of bits
Octal to binary : 0123 = 0 001 010 011 = 1010011
Hex to binary : 0x156 = 0001 0101 0110 = 101010110
Original code 、 Inverse code 、 Complement code
- For signed numbers :
- The highest bit of binary is the signed bit :0 It means a positive number ,1 It's a negative number
- The original code of a positive number 、 Inverse code 、 The complement is the same
- 0 The inverse of 、 The complements are all 0
- The inverse of a negative number = Its original symbol bit remains unchanged , Reverse other bits
- A negative complement = Its inverse +1
- When the computer is working , They all operate in the form of complement
1 -1
Original code 0000 0001 Original code 1000 0001
Inverse code 0000 0001 Inverse code 1111 1110
Complement code 0000 0001 Complement code 1111 1111
An operator
| Operator | describe | The rules |
|---|---|---|
| & | Bitwise AND | Also for 1, The result is 1, Otherwise 0 |
| | | Press bit or | There is one for 1, The result is 1, Otherwise 0 |
| ^ | Bitwise XOR | When the result is different, the result is 1, Otherwise 0 |
| << | Move left | Move left N Bits are multiplied by 2 Of N Power |
| >> | Move left | Move right N Bits are divided by 2 Of N Power |
The essence of bit operation
As long as it's an operation , All complement !!! The end of the operation needs to be converted into the source code
// 2 Complement code 3 Complement code
2 & 3 = 0000 0010 & 0000 0011 = 0000 0010 = 2
2 | 3 = 0000 0010 | 0000 0011 = 0000 0011 = 3
2 ^ 3 = 0000 0010 ^ 0000 0011 = 0000 0001 = 1
// -2 Complement code 3 Complement code result ( Complement code ) -> Launch original code
-2 & 3 = 1111 1110 & 0000 0011 = 0000010 = 2
// Displacement operators The arrow moves in which direction
// Move right : The low overflow sign bit remains unchanged , And use the sign bit to fill the high bit of overflow
// Move left : The sign bits remain the same , Low complement 0
// Move left n position = Original value * 2^n Move right n position = Original value / 4
1 >> 2 = 0
1 << 2 = 0000 0100 = 4
thus , Bit operation has been explained . We can find that the function of bit operation is really just doing Mark nothing more , Is worthy of the name Bitwise operation .
边栏推荐
- JSON.stringify用法
- Vs debugging skills
- 内网学习笔记(8)
- 嵌入式开发:嵌入式基础——回调函数
- 【动态规划】—— 背包问题
- [tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (III)
- Infiltration learning diary day20
- Ali an interview question: use two threads to output letters and numbers alternately
- What else can PLM do?
- crane:字典项与关联数据处理的新思路
猜你喜欢
随机推荐
新华三的千亿企业梦,还得靠吃ICT老本来实现?
[acwing] explanation of the 57th weekly competition
Details of istio micro service governance grid traffic management core resource controller
云原生(三十) | Kubernetes篇之应用商店-Helm
What kind of air conditioner is this?
爱可可AI前沿推介(6.27)
思考的角度的差异
Intranet learning notes (8)
Implementation of recruitment website based on SSM
创建Deployment后,无法创建Pod问题处理
A pang's operation record
Quick news: Huawei launched the Hongmeng developer competition; Tencent conference released the "Wanshi Ruyi" plan
【问题解决】Tensorflow中run究竟运行了哪些节点?
Interviewer: do you understand redis' shared object pool?
Pre training weekly issue 51: reconstruction pre training, zero sample automatic fine tuning, one click call opt
同花顺能开户炒股吗?安全吗?
Awk concise tutorial
To understand again is the person in the song
Prometheus 2.26.0 new features
基于SSM实现招聘网站








![[weekly replay] the 81st biweekly match of leetcode](/img/66/03ee4dbb88b0be7486b71cd4059f44.png)