<1> before understanding the displacement, first understand the binary representation and relationship between positive and negative numbers:
Example 15 and-15:
15 original code: 00000000 00000000 00000000 00001111
Makeup: 11111111 11111111 11111111 11110000
+ 1 =
-15 original code: 11111111 11111111 11111111 11110001
The original code of a negative number is: the original code of a positive number is reversed, plus 1.
<2> displacement operation: (valid only for int type data. In java, the length of an int is always 32 bits, that is, four bytes, it operates on the binary number of the integer ). it can also be applied to the following types: byte, short, char, and long (of course, they are all integers ). For these four types, JVM converts them to the int type before performing operations.
<Move left
> Right shift
>>> Unsigned right shift
<And> is a numerical displacement,> is a logical displacement. [Note]: Java does not exist <.
$1> m <n meaning: shifts the binary number represented by integer m to n places left, removes n places from the high position, and adds 0 to the low position. (in this case, a positive number is changed to a negative number)
Instance:
3 <2 analysis:
3 Binary Format: 00000000 00000000 00000000 00000011, according to the principle of $1, 00000000 00000000 00000000 00001100, that is, 12.
Shift left to make the integer negative:
10737418 <8
10737418 binary representation: 00000000 10100011 11010111 00001010, according to the principle of $1, 10100011 11010111 00001010 00000000, that is,-1546188288.
$2> m> n: shifts the binary number represented by integer m to n places right. m is a positive number, and all values in the high position are 0. m is a negative number, and all values in the high position are 1.
Example:
3> 2 analysis:
3 Binary Format: 00000000 00000000 00000000 00000011, according to the principle of $2, 00000000 00000000 00000000 00000000, that is, 0.
-3> 2:
-3 binary format: 11111111 11111111 11111111 11111101, according to the principle of $2, get 11111111 11111111 11111111, that is,-1.
Above: each integer represents a 32-bit binary. If the 32-bit shifted to the right is the same as the 0-bit shifted to the right. And so on. The multiples of the right 32 are the same.
Note: For 32-bit shifted to the right and 0-bit shifted to the right, the result is the same, which I have never understood. Now I can only understand 32 as special. It is equivalent to full migration. It is the same as 0-bit shifting. The same is true for left shifting.
$3> m >>> n: the binary value represented by an integer m shifts n places to the right. Both positive and negative values add zeros to the upper positions.
Instance:
3 >>> 2 profiling:
3 Binary Format: 00000000 00000000 00000000 00000011, according to the principle of $3, 00000000 00000000 00000000 00000000, that is, 0.
-3> 2:
-3 binary format: 11111111 11111111 11111111 11111101, according to the principle of $3, 00111111 11111111 11111111, that is, 11111111.
[Note]: for $1, $2, $3, if n is a negative number, the JVM will first modulo n for 32 to a negative number with an absolute value less than 32, then add 32 until n becomes a positive number.
Instance:
4 <-10
The binary format of 4: 00000000 00000000 00000000 00000100, and the modulo of-10 on 32 is added with 32. Needless to say, if 22 is obtained, 4 <-10 is equivalent to 4 <22.
In this case, according to the principle of $1, 00000001 00000000 00000000 is obtained, and 00000000 is obtained.
OK.
To sum up:
M <n: that is, if the number does not overflow, for positive and negative numbers, the left shift of n digits is equivalent to m multiplied by the n power of 2.
M> n is equivalent to dividing m by the n power of 2, and the result is obtained as an integer. If the result is decimal, there are two situations: (1) if m is a positive number, the resulting chamber will discard the decimal place unconditionally; (2) if m is a negative number, discard the decimal part, then, add the integer plus 1 to get the value after the displacement.
---------------------------------------------------------------------------------
Next, let's talk about the advantages of bit operations, which are extremely fast. These are the underlying binary machine operation commands.
For example, a * 2,
1. jvm first allocates space for variable;
2. Perform the * 2 operation again;
3. Return the result to the corresponding variable.
A <1, like a * 2, only needs one command, and the speed is very fast. Of course, the first three displacement operations are multiples of 2.
Available for operation.
I would like to add some more. When talking about bitwise operations, of course we should also talk about four operators :~ (Not by bit), | (by bit OR), & (by bit
And), ^ (by bit OR), these are basic usage of the university computer, operate on the binary form of the integer, and then
Convert to an integer as follows.
1 .~ (Bitwise NOT): [meaning] returns the inverse of the binary form of the integer.
~ 4: (unary operator)
The binary format of 4 is: 00000000 00000000 00000000 00000100, which is obtained after bitwise inversion.
To: 11111111 11111111 11111111 11111011, that is,-5.
2. | (bitwise OR): [undefined] performs a logical or operation on the binary form of two integers in a bit by bit. The principle is: 1 | 0 = | 0 = | 1 = | 1 = 1 | 1 = 1
.
4 |-5:
The binary format of 4 is: 00000000 00000000 00000000 00000100,
-5 binary format: 11111111 11111111 11111111 11111011,
Perform logical or operation by bit: 11111111 11111111 11111111 11111111, that is, get-1.
3. & (bitwise AND): [meaning] perform logic and operation on the binary form of two integers in a bit by bit. Principle: 1 | 0 = | 0 = & 1 = 1; 0 & 1 = 0.
4 &-5:
The binary format of 4 is: 00000000 00000000 00000000 00000100,
-5 binary format: 11111111 11111111 11111111 11111011,
Logic and operation by bit: 00000000 00000000 00000000 00000000, that is, 0 is obtained.
Practical application: you can convert bytes to an integer in the format of-64 & 0xFF = 192 or octal,-64 & 0377 = 192,
In fact, both 0xFF and 0377 represent integer 255,
4. ^ (bitwise exclusive or): [undefined] perform logical exclusive or operations on the binary form of two integers by bit. Principle: 1 ^ 1 = 0 ^ 0 = 1 0 ^ 1 = 1 0 ^ 0 = 0.
4 ^-5:
The binary format of 4 is: 00000000 00000000 00000000 00000100,
-5 binary format: 11111111 11111111 11111111 11111011,
Perform logical exclusive or operation by bit: 11111111 11111111 11111111 11111111, that is, get-1.
Practical application: it can be used to compare two numbers by bit or by the principle of 1 ^ 1 = 0 0 ^ 0 = 0. 20 ^ 20 = 0
From: http://aokunsang.javaeye.com/blog/615658
Binary descriptions:
Binary representation of negative numbers
Today we know how to express the negative binary method:-5
Step 1: first, convert 5 to the binary form of 101.
Step 2: In addition, the bitwise inversion is performed, (the front is all 1) 010
Step 3: Add 1 at the end to form: 11111111 11111111 11111111 11111011
If we convert the binary value with the highest bit of 1 into a negative integer
Step 1: bitwise inversion to 00000000 00000000 00000000 00000100
Step 2: Add 1 in the second bit to form 101
Step 3: Create an integer 5 with a negative number;
How to use code to convert binary data into decimal data in java?
| The code is as follows: |
Copy code |
Public int binary0000int (String str)
{
Int j = 0, I = 0;
Char c;
For (I = 0; I <str. length (); I ++)
{
If (str. charAt (str. length ()-I) = '1 ')
{
J = j + exp (2 * ln (str. length ()-I ));
}
}
Return j;
}
|
Transferred from:
Http://www.111cn.net
(1) positive and negative representation: "1" indicates "positive", "0" indicates "negative" (2) which code is used to store digits in a computer? Complement (3) convert the negative binary complement to the decimal method 1. Replace the complement code with "anti" (replace "1" with "0 ", "0" for "1 ". For example, "101010" is reversed and then "010101 ") 2. "add 1" to the binary number after the inversion. 3. Use the conventional method to convert the binary number after "add 1" to the decimal number and convert the negative number to the binary number.
Http://blog.111cn.net/onewalkingman/archive/2009/01/10/3746154.aspx
We already know that all data in a computer is expressed in binary.
We have also learned how to convert a decimal number to a binary number.
However, we still haven't learned how to express a negative number in binary.
For example, suppose there is a number of int type, the value is 5, then we know that it is represented in the computer:
00000000 00000000 00000000 00000101
5 is converted to a binary system of 101, but the number of int types occupies 4 bytes (32 bits), so a bunch of 0 values are filled in front.
Now I want to know how-5 is represented in a computer?
In a computer, a negative number is expressed in the form of a positive complement.
What is a supplemental code? This should start with the original code and the reverse code.
Original Code: an integer that is converted into binary numbers based on the absolute value. It is called the original code.
For example, 00000000 00000000 00000000 00000101 is the source code of 5.
Reverse Code: returns the bitwise result of the binary number. The new binary number obtained is called the reverse code of the original binary number.
The reverse operation indicates that the original value is 1, and the value is 0. The original value is 0, and the value is 1. (1 to 0; 0 to 1)
For example, if the bitwise of 00000000 00000000 00000000 is reversed, 00000101 11111111 11111111 is obtained.
Said: 11111111 11111111 11111111 11111010 00000000 is an anti-code of 00000000 00000000 00000101.
The anti-code is mutual, so it can also be called:
11111111, 11111111, 11111111, 11111010, 00000000, 00000000, 00000000, 00000101, and are mutually inverse codes.
Complement: the anti-code plus 1 is called a complement.
That is to say, to get a complement of a number, first obtain the reverse code, and then add 1 to the reverse code. The resulting number is called a complement.
For example, the 00000000 00000000 00000000 00000101 anti-code is: 11111111 11111111 11111111 11111010.
Then, the complement code is:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111
Therefore,-5 is expressed in the computer as 11111111 11111111 11111111 11111011. Convert to hexadecimal: 0 xFFFFFFFB.
Let's look at how integer-1 is represented in a computer.
Suppose this is also an int type, then:
1. First obtain the original code of 1: 00000000 00000000 00000000 0000000111111111 11111111 11111111 11111110
2. Reverse Code: 11111111 11111111 11111111 11111110
3. Supplemental code: 11111111 11111111 11111111 11111111
It can be seen that the binary expression of-1 in a computer is full 1. Hexadecimal: 0 xFFFFFF
Personal summary:
Complement: the anti-code plus 1 is called a complement.
Convert a negative binary to decimal
Example: 11111111 11111111 11111111 11111110
Step 1: Reduce 1 to 11111111 11111111 11111111 11111101 (binary, when the first digit is insufficient, the first digit is also 2)
Step 2: Remove the 00000000 00000000 00000000 00000010
Convert to decimal according to common methods. Add the-number at the end.
About java ~ Non-Bitwise
For example :~ 1
1 Binary is 00000000 00000000 00000000 00000001
~ 1: 11111111 11111111 11111111 11111110
The first value is 1, indicating that the number is-the result can be calculated based on the above calculation method.