当前位置:网站首页>Binary original code, inverse code, complement code
Binary original code, inverse code, complement code
2022-07-24 10:07:00 【Deciduous ex】
Binary source code 、 Inverse code 、 Complement code
I used to see binary source code in College 、 Inverse code 、 When complementing these, it is almost a glance , Because I think it has nothing to do with upper level development , Now, because there is a project to do the encapsulation of audio stream , You need to operate on binary , Let's review .
Any data in the computer is stored in binary , For a number , The computer should use a certain encoding method for storage , Original code 、 Inverse code 、 Complement is the way that a machine stores a specific number .
Original code
The source code is the easiest way for human brain to understand and calculate
Is the absolute value of the sign bit plus the value , The first bit represents the sign bit , The rest of the bits represent the value . Comparison 8 Bit binary :
+1=0000 0001
-1=1000 0001
Because the first sign bit is occupied , therefore 8 The binary value range of bits is :
[1111 1111 , 0111 1111]
namely
[-127 , 127]
Inverse code
Complement representation the human brain cannot intuitively see its value . It is also necessary to convert the original code to calculate its value .
The inverse of a positive number is itself
The inverse of a negative number is based on its original code , The sign bits remain the same , The rest of the bits are reversed .
+1= 0000 0001 primary = 0000 0001 back
-1= 1000 0001 primary = 1111 1110 back
Complement code
Complement representation the human brain cannot intuitively see its value . It is also necessary to convert the original code to calculate its value .
The complement of a positive number is itself
The complement of a negative number is based on its inverse , Last +1.
+1= 0000 0001 primary = 0000 0001 back = 0000 0001 repair
-1= 1000 0001 primary = 1111 1110 back = 1111 1111 repair
Meaning of existence
Since the original code is best for human brain to read directly , Then why are there inverse codes and complements , Because when it comes to calculation , When the addition and subtraction of the original code symbol bits are involved, it will make the addition and subtraction extremely complex .
for example :1-1=1+(-1)=0000 0001 + 1000 0001 =1000 0010 = -2 This is obviously wrong
Inverse code calculation
When using the original code for calculation, subtraction will lead to an error in the calculation result , People began to study how to involve symbolic bits in operations . That is, the inverse code .
1-1=1+(-1)=0000 0001 + 1111 1110 = 1111 1111 back = 1000 0000 primary =-0=0
In this way, the problem that the original code cannot be subtracted has been solved , But there is a drawback , Namely
0When , In human understanding ,+0 and -0 It's the same , But it may appear in the complement 0000 0000 and 1000 0000 Two codes represent 0 The situation of , So the complement appears .Complement calculation
1-1=1+(-1)=0000 0001 + 1111 1111 = 0000 0000 repair = 0000 0000 primary =0
Use complement , It's not just fixed 0 There are two coding problems , It can also represent a minimum number . That's why 8 Bit binary , The range represented by the original code or the inverse code is [-127, +127], And the range represented by complement is [-128, 127].
Because the machine uses complement , So for the programming commonly used in 32 position int type , The range can be expressed as : [-231, 231-1] Because the first bit represents the sign bit . When using the complement representation, you can save another minimum value .
Be careful
stay java Chinese binary is encoded by complement , So we can often see such a saying :
Java in Byte The value range of the type is -128~127
Many people, including me, were confused when I studied this field , Why not symmetrical , Generally speaking, I don't explain it too carefully, and it's easy to forget , Knowing the principle of complement coding here is much easier to understand .
8 The value range of bit source code and inverse code is -127~127, It's a number less than the complement , Why is that , Because just now I mentioned that there are +0 and -0 Divided into , Therefore, the value range is just symmetrical , And in the complement , There is only 0 This number . So you can empty Give one digit to indicate other numbers .
So here comes the question , Why does it happen to be -128 Well , It can be deduced as follows .
primary 1111 1101= back 1000 0010= repair 1000 0011=-125
primary 1111 1110= back 1000 0001= repair 1000 0010=-126
primary 1111 1111= back 1000 0000= repair 1000 0001=-127
Just look at whether the complement has found any rules
Then the value of our complement is :1000 0000 How much should it be
According to the above rules, we can see repair 1000 0000=-128
summary
Finally, I have solved the problem that has been bothering me for a long time , Now let's understand a lot about byte The calculation of binary correlation is much clearer .
边栏推荐
- JMeter setting default startup Chinese
- Selnium checks three conditions when it cannot locate an element
- PHP Basics - session control - cookies
- Linux deployment mysql8.0
- Where is the bitbucket clone address
- Getting started with identityserver4
- Tencent 5g innovation center was established, laying out key directions such as unmanned ports, smart mines and E-sports events
- Write a simple memo using localstorage
- JS bind simulation
- The best time to buy and sell stocks Ⅳ (leetcode-188)
猜你喜欢

高精尖中心论文入选国际顶会ACL 2022,进一步拓展长安链隐私计算能力

The best time to buy and sell stocks Ⅲ (leetcode-123)

Spark Learning: Spark implementation of distcp

Curse of knowledge

Spark Learning: how to choose different association forms and mechanisms?

Implementation and traversal of binary tree and binary tree sorting tree

程序的编译与链接

Add SSH key to bitbucket

Dr. water 3

Embedded development: Tools - optimizing firmware using DRT
随机推荐
[STM32 learning] (14) two 74HC595 controls four nixie tube displays
2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices
[example] v-contextmenu right click menu component
[STM32 learning] (7) use of serial port 2 (usart2)
Raspberry Pie: the serial port has been unable to read the information sent by the upper computer
2022, enterprise informatization construction based on Unified Process Platform refers to thubierv0.1
Compilation and linking of programs
Reading makes people improve my list
Scala learning: why emphasize immutable objects?
[STM32 learning] (5) press the key to control the flow light (interrupt Implementation)
Aggregate log server
Deployment and analysis of coredns
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus
2022, will lead the implementation of operation and maintenance priority strategy
Countdownlatch and join [concurrent programming]
Spark Learning: implement compact table command
What's the difference between testing / developing programmers' professionalism and salted fish? They don't want to be excellent coders?
Anti shake and throttling
MySQL status view qps/tps/ cache hit rate view
ThreeJs