Key Words: Java Shift Operations
Younger brother in the beginning, but also ask you to take care of.
Reproduced:
Http://blog.csdn.net/wangming7306/archive/2007/05/22/1621609.aspx
Java Shift Operations (collection + correction + typesetting)
This collection from the network collection, is my collation, correction and typesetting, to provide you with a comprehensive knowledge of the explanation, some places may not be good, hope understanding.
======================================================================================
The Java median operator includes:
&
|
~
^
<<
>>
>>>
One
The first step is to figure out the number of digits involved in the operation:
(
Lenovo: Java's 8 basic types: Byte,short, char, int, Long,float,double,boolean.
Fixed length in memory (bytes): 1 2 2 4 8 4 8 True/false
The length of these fixed types is independent of the specific hardware and software environment. Unlike C + +, the char type in Java is stored in Unicode code
In response, Java provides 8 types of wrappers:
Byte,short,character,integer,long,float,double,boolean.
The reciprocal conversion between them: for example:
Double a=1;
Converts a double base type to a double wrapper type
Double B=new double (a);
Converts a double wrapper type to a double base type
A=b.doublevalue ();
)
So the int is 32 bits. Long is 64 digits.
such as int i = 1;
The binary original code of I is expressed as:
00000000000000000000000000000001
Long L = 1;
The binary original code of L is expressed as:
0000000000000000000000000000000000000000000000000000000000000001
Two
The original code--the sign bit is 0 to indicate a positive number, and 1 is a negative number;
The rest of you are equal to the absolute value of the truth.
such as: 0000000000000010b=2,1000000000000010b=-2
Anti-code--The use of sign bits and the representation of positive numbers is the same as "original code";
The representation of a negative number is based on the "original code" representation by putting the symbol bit outside the
The people who take the reverse to obtain.
such as: 0000000000000010b=2,1111111111111101b=-2
Complement--The use of sign bits and the representation of positive numbers is the same as "original code";
The representation of negative numbers is obtained by adding one on the basis of "inverse code".
such as: 00000010b=2,11111110b=-2
such as int i =-1;
10000000000000000000000000000001, the highest bit is the sign bit. A positive number is 0 and a negative number is 1.
The symbol bit is unchanged, and the other bits are reversed after a bit:
11111111111111111111111111111110, that is, the inverse code.
Counter code plus 1:
11111111111111111111111111111111, that is, the complement.
==================================================================
Note: Negative numbers are involved in the operation with the complement. Get is also the complement, need to subtract 1 to get the original code.
Be sure to understand this sentence!!!
==================================================================
Three
The commonly used bitwise operator 0 is more specific in the in-place operation.
& and. All 1 is 1, and 0 is 0. Any number with 0 equals 0.
| Or. 1 is 1, all 0 is 0. Any number and 0 are equal to the original value.
~ Not. Bitwise counter
^ xor or. The same is 0, the difference is 1. Any number with 0 is equal to the original value.
for type int data:
1.<<
Logic left, 0 to the right, and the symbol bit is the bit that is moved to.
Positive:
X<<1 is generally equivalent to 2x, but may overflow.
If x is in this range: 2 of 30 times (2 of 31 of the square-1) binary represents 0100...0000 to 0111 ... After the 1111,<<, the maximum is 1, and it becomes negative.
Negative:
X<<1 is also equivalent to 2x, it is possible to overflow.
If x is in this range:-2 of the 31-~-(2 of 30-square + 1) binary represents 1000 ... 0000 to 1011 ... After the 1111,<<, the maximum is 0, which becomes a positive number.
2.>>
Arithmetic shifts to the right, and does not correspond to the above, when the left is 0 for a positive number, and 1 for the left.
X>>1, the equivalent of X/2, the remainder is discarded, because this is narrowing, so it will not overflow.
But one thing to note: 1 to the right, how many digits are 1. (The truth is very simple, hehe)
In addition, the remainder that is discarded is positive:
The remainder that 3>>1=1 discards is 1.
The remainder of the -3>>1=-2 is also 1, not 1.
For positive x>>1 and X/2 equality
Negative x>>1 and X/2 are not necessarily equal.
3.>>>
Logical move right, this is the corresponding to the <<
This moves the symbol bit together, 0 on the left.
For positive,>>> and >> is the same
For negative numbers, the right shift is followed by a positive number.
You can use integer.tobinarystring (int i) to see 01 bits, more intuitive.
Four
Negative numbers participate in the operation, get the complement, negative numbers to get the original code method:
Method One: The complement first minus 1, and then the bit by the counter, get the original code. That is the result of the operation.
Method Two: will be the complement of the first bit by the counter, plus 1, to get the original code. That is the result of the operation.
With 0 exceptions, if you get 0, you do not need either of these methods, which is the original code bit 0.
In addition, two positive numbers after the operation of the original code, do not need to use the method to find the original code.
Example:
-1^1,
-1
10000000000000000000000000000001--Original Code
11111111111111111111111111111110--Anti-code
11111111111111111111111111111111--complement
1
00000000000000000000000000000001--Original Code
Then -1^1 equals
11111111111111111111111111111111^
00000000000000000000000000000001=
11111111111111111111111111111110--complement
11111111111111111111111111111101--Anti-code
10000000000000000000000000000010--Original Code ==-2
namely -1^1=-2
Example:
-2^1
-2
10000000000000000000000000000010--Original Code
11111111111111111111111111111101--Anti-code
11111111111111111111111111111110--complement
1
00000000000000000000000000000001--Original Code
Then -2^-1 equals
11111111111111111111111111111110^
00000000000000000000000000000001=
11111111111111111111111111111111--complement
11111111111111111111111111111110--Anti-code
10000000000000000000000000000001--Original Code ==-1
The following is Cooltigerzsh (Apollo) at 2005-2-4 15:16:07 to (<<, >>, >>>) a turn to explain:
The
shift operator is oriented to an Operation object that is also a binary "bit". You can use them individually to handle integer types (one of the main types). The
Left shift operator (<<) can move the operand on the left side of the operator to the right of the specified number of digits (0 in low) on the right-hand side of the operator. The
Signed right shift operator (>>) assigns the Operation object to the left of the operator to the right of the specified number of digits to the right-hand operator. The
signed right shift operator uses the symbol extension: If the value is positive, insert 0 at the high level, or 1 in the high position if the value is negative.
Java also adds a "unsigned" right shift operator (>>>) that uses "0 extensions": Inserts 0 at the high level, whether positive or negative.
This operator is not in C or C. If Char,byte or short are shifted, they are automatically converted to an int before the shift occurs. The
only 5 lows on the right side will be used. This prevents us from moving an unrealistic number of digits in an int.
If a long value is processed, the resulting result is long. Only the 6 lows on the right are used at this point to prevent moving more than the number of bits in the Long value.
But you may also experience a problem when you make an unsigned right shift. If you perform a right shift operation on a byte or a short value,
may not get the correct result (Java 1.0 and Java 1.1 are particularly prominent). They are automatically converted to type int and are shifted to the right.
But "0 extensions" do not occur, so in those cases you get the result of 1.
Such as:
public class Urshift {
public static void Main (string[] args) {
int i =-1;
I >>>= 10;
System.out.println (i);
Long L =-1;
L >>>= 10;
System.out.println (l);
Short S =-1;
S >>>= 10;
System.out.println (s);
byte B =-1;
b >>>= 10;
System.out.println (b);
}
}
Output results:
4194303
18014398509481983
-1
-1
===============================================================================
Also have to mention, is also very implicit point, that is I found on Einstein blog, he said is SCJP on the topic,
Excerpts from his article are as follows:
SCJP's problem is really "karma" ah, a lot of people can not think of the problem, a little meaning. Haha, the last one today, after lying down, too late,
Tomorrow or go to Shenyang to sell digital cameras (excited ing ...)
The following code:
Class test002
{
public static void Main (string[] agrs)
{
int i=-1;
int j=i>>>32;
System.out.println (j);
}
}
According to my understanding should output: 0, because the Java type of int is 4 bytes, that is to say, 32 bits, when the right move 32 digits, all the bits should all turn to 0, but the output is:-1,
Thought for a long time did not want to understand on the internet posted a post asked, thank coffer283 and Danieljill () two friends.
Originally in the Java shift operation because INT is accounted for 32 bits, the number of shifts is 32 of the modulus, so when the i>>>32 is equal to i>>>0, equivalent to no shift.
I tried again. A long type of shift, which is 8 bytes or 64 bits, so the number of shifts is modulo 64.
---------------------------------------------------------------------------------------------------
The above is his question, has given me a lot of inspiration, to the Java shift operation has the deep layer understanding.
At the same time, I also experimented with the shift cycle of the Byte,short type, which is also 32, the same as the int type, thus verifying that byte and short are shifted to the right, automatically converted to int type, and I validated the <<, >>, >> > All 3 shift operators follow the shift cycle.
=======================================================================================
Oh, now I finally finished, the Java shift operation has a comprehensive understanding. I feel no white flowers these hours sorted, hoho~~