当前位置:网站首页>Study notes: original code, inverse code, complement code
Study notes: original code, inverse code, complement code
2022-07-26 01:37:00 【Laughing shrimp】
Learning notes : Original code , Inverse code , Complement code
As a programmer , Always adhere to a belief , That's it Save farts and blow the lamp , This is also the source of everything .
- CPU Adding is the fastest .
- Try to convert subtraction operation into addition for calculation .
- When binary represents numeric value , The highest is the sign bit , If you need to judge the symbol before calculating , It's complicated . Ideas , Let the symbols participate in the calculation ( Like normal addition )
Original code
- With
8 positionBinary to represent aNumbersFor example . The leftmost number1 positionAsSign bit, After the rest7 positionIt can be used to expressvalue.
1.1.Positive numbersSymbol bit :0: Such as : 1 The binary of is 00000001
1.2.negativeSymbol bit :1: Such as :-1 The binary of is 10000001 Positive numbersOf Original code = Inverse code = Complement code . So we're talking aboutInverse code , Complement codeIt mainly meansnegative.- The reason why we invented
Inverse code , Complement codeJust to makenegativeBinary operation of , andPositive numbersKeep the same operation rules ( Give WaySign bitParticipate in the calculation and willSubtractionTurn intoAddConduct ), Finally, we can get the right result we want .
Inverse code
- Positive numbers : emphasize I've already said that
Positive numbersOfOriginal code 、 Inverse code 、 Complement codeAre all the same . - negative :
Sign bitunchanged ,restEach bitTake the opposite.
2.1.-1Original code :1000 0001
2.2.-1Complement code :1111 1110 ( Put it in256This value is254leaveComplement numberStill bad1) - Use
Inverse codeLetSign bitAlso participated in the calculation , But there are still two problems :
3.1. We get less results than the real results1.
3.2. Save an embarrassingnegative 0problem :1000 0000
Complement code
- Positive numbers : Again I've already said that
Positive numbersOfOriginal code 、 Inverse code 、 Complement codeAre all the same . - negative : stay
Inverse codeOn the basis of+1. Add the less inverse code operation1, And makePlus or minus 0The complement of is the same , It's solved-0problem .-0Original code :1000 0000-0Inverse code :1111 1111 ( This value is parsed as the original code255)-0Complement code :0000 0000 be equal to+0= 0000 0000 ( Sign bitoverflow, Back to a positive number ) - Artificial definition 1000 0000 by
-128, So that what we seeValue rangeNegative numbers will be one more than positive numbers :[-128, 127]
principle
With The clock For example , The current hour hand points to 6 spot , If you want to adjust it to 3 spot , There can be two options :
- Adjust clockwise :
+9namely6+9=3. - Counterclockwise adjustment :
-3namely6-3=3. To equivalent addition :6+(12-3)
Success will subtract -3 Replace with +9 The result is the same . Because they are in the same 12 In the closed loop , Complement each other .
our Complement code It's the same thing . 0000 0000 this 8 position Binary number is a 256 Big ring .( The maximum value is 255 add 1 To 256 It's the next round 0 了 , Just like there is no tonight 24 spot , Only tomorrow 0 spot )
example : stay 256 In the circle of , backward -1 Equal to forward +255.
(+255 yes -1 With 256 For the mold Complement number )-1 Original code :1000 0001-1 Inverse code :1111 1110 ( Put it in 256 This value is 254 Complementary number 255 Still bad 1 )-1 Complement code :1111 1111. stay Inverse code above +1 Just right +255.
Conclusion : subtract a number , be equal to add This is the number Complement code . because Complement code Convenience of calculation , So what is actually stored in the computer is Complement code .
Reference material
Zhang Ziqiu's blog : Original code , Inverse code , Complement code Detailed explanation
边栏推荐
- Linked list related interview questions
- Big view +500 cases, software teams should improve R & D efficiency in this way
- Network layer 2 and layer 3 forwarding
- Machine learning: Bayesian Networks
- Is it safe to open an account for stock speculation through the online account manager?
- Stack Title: basic calculator
- The best way to practice Animation: cover transition
- NiO simple example
- IP address of the network
- Should we test the Dao layer?
猜你喜欢

Modify CSV

Leetcode537. 复数乘法(可以,已解决)

CPU的三种模式
![[go] III. The simplest restful API server](/img/1f/f6fc8cc9a3891d01a25e709170188d.png)
[go] III. The simplest restful API server

Cross linguistic transfer of correlations between parts of speech and Gazette Features Reading Notes

The second China rust developer conference is coming, and the complete agenda has been exposed!

【ICKIM 2022】第四届知识与信息管理国际会议
![4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]](/img/95/5b9a2347d20cc5da0d2920b7f583ce.png)
4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]

Leetcode 537. complex multiplication (netizens' thoughts, ashamed)

Iftnews | suppose this is what the metauniverse looks like 20 years later
随机推荐
Recommend a super good UI automation tool: uiautomator2!
4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]
The second China rust developer conference is coming, and the complete agenda has been exposed!
Integer data type in C language (do you really understand it)
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 personal problem sets
3、 Pinda general permission system__ pd-tools-swagger2
zeromq浅析
Oracle - isupplier portal Invoicing error
Machine learning: Bayesian Networks
Ideal Path(UVA - 1599)
Fastjason handles generics
FreeBSD bnxt以太网驱动源码阅读记录二:
Analysis of zeromq
I just test it
Leetcode 537. 复数乘法(网友思路,自愧不如)
ABC find 4-cycle (pigeon nest theorem)
Huawei wireless device WDS configuration command
推荐⼀款超好⽤的UI⾃动化⼯具: UiAutomator2!
3059. Sculpture (jzoj)
PyCharm在创建py文件时自动添加头部注释