The difference between the source code, the reverse code, and the supplementary code in Java >>>> and the reverse code.
When I analyzed the HashMap hash algorithm two days ago, I met the >>and >> symbols. At that time, I checked the information and passed it in my mind. Today I met again. I did not expect to forget 0-0 ........
I have no memory. I had to take a note and remind myself that it is better to remember what I encounter than to remember a bad blog ~
Since in-place operations are involved, we will first learn from the most basic source code, complement code, and reverse code. Search this information, found a special introduction to this article, write is very thorough, then directly cited, the original address is: http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
1. Before learning the original code, anti-code, and complement code, you must first understand the concept of the machine number and real value.
1. Number of machines
The binary representation of a number in a computer, called the number of machines. The number of machines is signed. in a computer, the highest bit of a number is used to store the symbols. The positive number is 0, and the negative number is 1.
For example, if the number is + 3 in decimal format and the computer word length is 8 bits, the conversion to binary format is 00000011. -3 means 10000011.
In this case, 00000011 and 10000011 are the number of machines.
2. True Value
Because the first digit is the symbol bit, the form value of the machine number is not equal to the real value. For example, if the above signed number is 10000011, its highest bit 1 indicates negative, and its real value is-3 instead of the form value 131 (10000011 is converted to decimal equal to 131 ). Therefore, for the sake of distinction, the real value corresponding to the number of machines with signs is called the true value of the number of machines.
Example: the true value of 0000 0001 = + 000 0001 = + 0001 0001 =-000 =-1
II. the basic concepts and calculation methods of the original code, back code, and complement code before exploring why the machine needs to use the complement code, let's first understand the concept of the original code, back code, and complement code. for a number, the computer must use a certain encoding method for storage. the source code, the reverse code, and the supplementary code are used to store a specific number.
1. Original code
The original code is the absolute value of the symbol bit plus the true value, that is, the first digit represents the symbol, and the other digits represent the value. For example, if it is an 8-bit binary:
[+ 1] original = 0000 0001
[-1] original = 1000 0001
The first digit is the symbol bit. Because the first digit is the symbol bit, the value range of the 8-bit binary number is:
[1111 1111,011 1 1111]
That is
[-127,127]
The original code is the easiest way for the human brain to understand and calculate.
2. Anti-code
The expression of the anti-code is as follows:
The inverse code of positive numbers is itself
The negative number is based on the original code, the symbol bit remains unchanged, and the other bits are reversed.
[+ 1] = [00000001] original = [00000001]-
[-1] = [10000001] original = [11111110] Reverse
It can be seen that if an anticode represents a negative number, the human brain cannot intuitively see its value. It is usually converted to the original code and then computed.
3. Complement
The expression of the complement code is as follows:
Positive complement is itself
The complement of a negative number is based on the original code, the symbol bit remains unchanged, and the rest of you get the reverse code. Finally, + 1 (that is, + 1 on the basis of the reverse code)
[+ 1] = [00000001] original = [00000001] inverse = [00000001] Complement
[-1] = [10000001] original = [11111110] = [11111111]
For negative numbers, the human brain cannot directly see the value of the complement expression. It is usually necessary to convert the value to the original code to calculate its value.
Iii. Why should I use the original code, reverse code, and complement code?
Before I start to study in depth, I suggest "memorizing" the original code, the expression of the reverse code and the complement code, and the calculation method.
Now we know that the computer can have three encoding methods to represent a number. For positive numbers, the results of the three encoding methods are the same:
[+ 1] = [00000001] original = [00000001] inverse = [00000001] Complement
So there is no need to explain too much, but for negative numbers:
[-1] = [10000001] original = [11111110] = [11111111]
It can be seen that the original code, the reverse code and the complement code are completely different. Since the original code is directly recognized by the human brain and used for computation and representation, why is there a reverse code and a complement?
First, because the human brain can know that the first digit is the symbol bit, During computation, We will select the addition and subtraction of the true value area based on the symbol bit. (the concept of true value starts at the beginning of this article ). however, for computers, the addition and subtraction of multiplier is already the most basic operation, and the design should be as simple as possible. computerIdentifying the "symbol bit" clearly makes the computer's basic circuit design very complex.! So people came up with a method to include the symbol bit in the operation. we know that, according to the algorithm minus a positive number equals to adding a negative number, that is, 1-1 = 1 + (-1) = 0, soThe machine can only add but not subtractIn this way, the design of computer operations is simpler.
As a result, people began to explore the method of adding the symbol bit and only keep the addition. First, let's look at the original code:
Decimal expression: 1-1 = 0
1-1 = 1 + (-1) = [00000001] original + [10000001] original = [10000010] original =-2
If the original code is used for representation, the result is incorrect for subtraction. That is why the computerDo not use the original codeRepresents a number.
To solve the problem of original code subtractionReverse code:
Decimal expression: 1-1 = 0
1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] + [1111 1110] = [1111 1111] = [1000 0000] =-0
It is found that the negative code is used to calculate and subtract, and the true value of the result is correct. the only problem actually occurs in the special value "0. although we understand that + 0 is the same as-0, 0 is meaningless. in addition, there will be two encoding formats: [0000 0000] original and [1000 0000] original, indicating 0.
So the occurrence of the complement code,Solved the problem of zero symbols and two encodings.:
1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] supplement + [1111 1111] Supplement = [0000 0000 0000] Supplement = [0000] original
In this way, 0 is represented by [0000 0000], and-0, which previously encountered a problem, does not exist. In addition, [1000 0000] can be used to represent-128:
(-1) + (-127) = [1000 0001 1111] original + [1111 1111 1111] original = [1000 0001] supplement + [1000 0000] Supplement = []
-The result of 1-127 should be-128. in the result of the complement operation, [1000 0000] is-128. however, because the previous-0 complement code is used to represent-128,-128 does not represent the original code or reverse code. ("-128" indicates that the source code [1000 0000] is [0000 0000], which is incorrect)
Using the complement Code not only fixes the zero sign and two encodings, but also represents a minimum number. this is why the range of the 8-bit binary code expressed by the original code or the back code is [-127, + 127], and the range indicated by the complement code is [-128,127].
Because the machine uses the complement code, the 32-bit int type commonly used in programming can be expressed in the range of [-231,231-1] Because the first digit represents the symbol bit. you can save one more minimum value when using the complement representation.
Four original codes, anti-code, re-coding
What mathematical principles does a computer use to skillfully calculate symbols and convert subtraction into addition?
Think of a clock as a one-digit 12-digit number. If the current time is, What should I do if I want to set the time? We can:
1. 2 hours for callback: 6-2 = 4
2. Forward 10 hours: (6 + 10) mod 12 = 4
3. Forward dial 10 + 12 = 22 hours: (6 + 22) mod 12 = 4
Mod in method 2 and 3 refers to the modulo operation. 16 mod 12 = 4 means the remainder after dividing 16 by 12 is 4.
Therefore, you can use forward dial (addition) to replace the result of clock push (subtraction!
Now the focus is on how to replace a negative number with a positive number. The above examples show some clues and discover some patterns. But mathematics is rigorous.
First, we will introduce a concept related to mathematics: Same
Same concept
Two integers a and B. If they are equal to the remainder obtained by dividing them by an integer m, they are called a and B.
As a comment B (mod m)
Read as a and B.
Example: 4 mod 12 = 4
16 mod 12 = 4
28 mod 12 = 4
So 4, 16, and 28 are the same as the modulo 12.
Negative modulo
It is very easy to perform mod operations on positive numbers. But what about negative numbers?
The mathematical definitions of mod operations are as follows:
X mod y = x-y L x/y J
The formula above indicates:
X mod y equals x minus y multiplied by the lower bound of the quotient of x and y.
Take-3 mod 2 as an example:
-3 mod 2
=-3-2xL-3/2 J
=-3-2xL-1.5J
=-3-2x (-2)
=-3 + 4 = 1
Therefore:
(-2) mod 12 = 12-2 = 10
(-4) mod 12 = 12-4 = 8
(-5) mod 12 = 12-5 = 7
Start proof
Back to the clock issue:
2 hours for callback = 10 hours for the previous dial
Callback 4 hours = previous dial 8 hours
Call back for 5 hours = dial before 7 hours
Note: The Rule found here!
In combination with the concept of remainder learned above. In fact:
(-2) mod 12 = 10
10 mod 12 = 10
-2 and 10 are the same.
(-4) mod 12 = 8
8 mod 12 = 8
-4 and 8 are the same.
The distance from success is getting closer and closer. To replace negative numbers with positive numbers, you only need to use two theorems of the same remainder:
Anti-body:
A challenge a (mod m)
This theorem is very obvious.
Linear operation theorem:
If a branch B (mod m), c Branch d (mod m) then:
(1) a ± c ≡ B ± d (mod m)
(2) a * c branch B * d (mod m)
If you want to see the proof of this theorem, see: http://baike.baidu.com/view/79282.htm
Therefore:
7 release 7 (mod 12)
(-2) limit 10 (mod 12)
7-2 release 7 + 10 (mod 12)
Now we find a negative number with the same positive number as the remainder. but it is not 7-2 = 7 + 10, but 7-2 then 7 + 10 (mod 12), that is, the remainder of the calculation result is equal.
Next, let's go back to the binary problem. Let's take a look at the problem: 2-1 = 1.
2-1 = 2 + (-1) = [0000 0010 1000] original + [0001 0000] original = [0010 1111] Reverse + [1110] Reverse
First to this step, the anti-code of-1 indicates 1111 1110. if [1111 1110] is considered as the source code, [1111 1110] is considered as-126. Here, the symbol bit is removed, that is, 126.
The following rule is found:
(-1) mod 127 = 126
126 mod 127 = 126
That is:
(-1) limit 126 (mod 127)
2-1 limit 2 + 126 (mod 127)
The result of the remainder of 2-1 and 2 + 126 is the same! The remainder officially calculates the expected result: 2-1 = 1
So the anticode of a number is actually the number of the same remainder of a film. The film is not our binary, but the maximum value we can represent! This is the same as a clock. After turning around, you can always find a correct value in the displayed range!
The 2 + 126 is clearly equivalent to switching around the clock, and because the symbol bit is involved in the calculation, it is exactly the same as the overflow highest bit to form the correct calculation result.
Since anticode can convert subtraction into addition, what is the complement code used by the computer now? Why can we still get the correct result by adding 1 on the basis of the anti-code?
2-1 = 2 + (-1) = [0000 0010 1000] original + [0001 0000] original = [0010 1111] supplement + [1111] supplement
If [1111 1111] is used as the source code and the symbol bit is removed, then:
[0111 1111] original = 127
In fact, on the basis of the reverse code + 1, it is equivalent to adding the value of the membrane:
(-1) mod 128 = 127
127 mod 128 = 127
2-1 limit 2 + 127 (mod 128)
At this time, the dial is equivalent to converting every 128 scales. Therefore, the minimum and maximum values of the computation result indicated by the complement code should be [-128,128].
However, due to the special case of 0, there is no way to represent 128. Therefore, the value range of the complement code is [-128,127].
Java> and>
At this point, we have already understood the basic knowledge. What is the difference between the Shift Operators> and> in Java?
All operations are performed on binary numbers.
1. Right Shift Operator> specifies the number of times that all bits of the specified value are shifted to the right. Do not discard the removed part on the right. Fill in the empty part on the left with the original number (this is the so-called shift right with a symbol)
-- For example, the value 5 is 00000010 after right shift.
-- Assume that x =-12 indicates that the 32-bit int type is 11111111111111111111111111110100.
X> 2: shifts the two digits to the right with the symbol. The result is 11111111111111111111111111111101, Which is decimal to-3.
2. The only difference between >>> and> is that it is filled with 0 regardless of the number on the left.
-- For example,-1 of the byte type is 8-bit 11111111 (complement representation)
B >>> 4 means the unsigned four places to the right, that is, 00001111, so the result is 15.
What is the reverse Code complement in JAVA?
The inverse code is to reverse the bitwise of each digit of the binary number.
That is, change 1 to 0 to 1.
The complement code is different.
The positive complement is the source code itself.
The complement of a negative number is the bitwise of each other except the symbol bit, and the symbol bit remains unchanged.
For example:
Decimal number 11:
Source code: 00001011;
Back code: 11110100;
Complement: 00001011;
Decimal-11:
Source code: 10001011;
Back code: 01110100;
Complement: 11110101;
The original and reverse codes of positive and negative numbers are different from the complement codes.
The original, complement, and reverse codes of positive numbers are the same.
The negative number is reversed by bit (except for the symbol bit)
The complement code is an anti-code + 1