Java shift operation (Collection + correction + layout)

Source: Internet
Author: User
Tags bitwise operators
Java shift operation (Collection + correction + layout)

This collection comes from network collection. It is my arrangement, correction, and layout to provide you with a comprehensive explanation of the knowledge. It may be difficult to arrange it in some places.
========================================================== ========================================================== ======
Java median operators include:
&
|
~
^
<
>
>>>

I,
First, we need to figure out the number of digits involved in the calculation:
(
Lenovo: eight basic types of Java: byte, short, Char, Int, long, float, double, Boolean.
Fixed Length (in bytes) in memory: 1 2 2 4 8 4 8 true/false
The length of these fixed types is independent of the specific software and hardware environment. This is different from C ++. the char type in Java is stored in Unicode code.

Java provides eight packaging types:
Byte, short, character, integer, long, float, double, Boolean.
Mutual conversion between them: for example:
Double A = 1;
// Convert the Basic double type to the double packaging type
Double B = new double ();
// Convert the double packaging type to the double basic type
A = B. doublevalue ();
)

So the int value is 32 bits. Long is 64-bit.

For example, int I = 1;
The binary original code of I is:
00000000000000000000000000000001

Long L = 1;
The binary original code of L is:
0000000000000000000000000000000000000000000000000000000000000001

II,
Original code -- the sign bit 0 indicates a positive number, and 1 indicates a negative number;
The rest are equivalent to the absolute values of true values.
Example: 0000000000000010b = 2, 000000000000010b =-2
Anticode-the usage of the symbol bit and the representation of positive numbers are the same as those of the "original code;
A negative number is expressed based on the original code
You get the opposite.
Example: 201710000000000000010b = 2, 11111111111101b =-2
Complement-the usage of the symbol bit and the representation of positive numbers are the same as those of the "original code;
The negative number is obtained by adding 1 on the basis of the "anticode.
Example: 100000010b = 2, 11111110b =-2

For example, int I =-1;
10000000000000000000000000000001, the highest bit is the symbol bit. The positive value is 0, and the negative value is 1.
The symbol bit remains unchanged, and the other bit is reversed by bit:
11111111111111111111111111111110, that is, the anti-code.
Anti-code plus 1:
11111111111111111111111111111111, that is, the complement code.
========================================================== ======================================
Note: negative numbers are all involved in the computation using the complement code. The obtained code is also a complementary code. You need to subtract 1 to obtain the original code.
You must understand this sentence !!!
========================================================== ======================================

III,
Common bitwise operators 0 are special in bitwise operations.
 
. All 1 is 1, and 0 is 0. Any number and 0 are equal to 0.
| Or. 1 is 1, and all 0 is 0. Any number is equal to zero or equal to the original value.
~ Not. Bitwise Inversion
^ Exclusive or. The same value is 0, and the difference is 1. Any number is different from zero or equal to the original value.

For int type data:
1. <
The logic is shifted to the left. The right side is supplemented with 0, and the symbol bit is the bit to be moved.
Positive number:
X <1 is generally equivalent to 2x, but may overflow.
If X is in this range: power 30 of 2 ~ (31 to the power of 2-1) binary representation of 0100... 0000 to 0111... 1111, <after which the highest value is 1, it becomes a negative number.
Negative Number:
X <1 is generally equivalent to 2x, and may overflow.
If X is in this range:-2 to the power of 31 ~ -(Power-30 of 2 + 1) binary representation of 1000... 0000 to 1011... 1111, <after which the maximum value is 0 and the value is a positive number.

2.>
The right shift of arithmetic. It does not match the above. If it is a positive number, the left side is supplemented by 0. If it is a negative number, the left side is supplemented by 1.
X> 1, which is equivalent to X/2. The remainder is discarded because it is reduced, so it will not overflow.
But pay attention to one thing:-1 shifts the number of places to the right is-1. (This is simple, huh, huh)
In addition, the remainder discarded is positive:
3> 1 = 1 the remainder of the discard is 1.
-3> 1 =-2 The remainder discarded is also 1, rather than-1.
Equal to positive numbers x> 1 and X/2
The numbers x> 1 and X/2 are not necessarily equal.

3. >>>
The logic is shifted to the right, which corresponds to <
This moves the symbol bits together, and adds 0 to the left.
For positive numbers, >>>and >>are the same.
For a negative number, after the right shift, it becomes a positive number.

You can use integer. tobinarystring (int I) to view 01 BITs, which is more intuitive.

IV,
A negative number is involved in the calculation and the result is a complementary code. The method for obtaining the original code from a negative number is as follows:
Method 1: First subtract 1 from the complement code, and then reverse the code by bit to obtain the original code. The operation result.
Method 2: Obtain the source code by bitwise AND adding 1. The operation result.
With the exception of 0, if the obtained value is 0, the two methods are not required, that is, the obtained original code bit is 0.
In addition, the original code is obtained after two positive numbers. You do not need to use the original code method.

Example:
-1 ^ 1,
-1
10000000000000000000000000000001 -- original code
11111111111111111111111111111110 -- anti-code
11111111111111111111111111111111 -- supplemental code
1
00000000000000000000000000000001 -- original code

-1 ^ 1 equals
11111111111111111111111111111111 ^
00000000000000000000000000000001 =
11111111111111111111111111111110 -- supplemental code
11111111111111111111111111111101 -- anti-code
10000000000000000000000000000010 -- original code =-2
-1 ^ 1 =-2

Example:
-2 ^ 1
-2
10000000000000000000000000000010 -- original code
11111111111111111111111111111101 -- anti-code
11111111111111111111111111111110 -- supplemental code
1
00000000000000000000000000000001 -- original code
-2 ^-1 equals
11111111111111111111111111111110 ^
00000000000000000000000000000001 =
11111111111111111111111111111111 -- supplemental code
11111111111111111111111111111110 -- anti-code
10000000000000000000000000000001 -- original code =-1

The following is an explanation of cooltigerzsh (Apollo) at 15:16:07 (<, >>, >>>:

The operation object oriented to the shift operator is also the binary "bit ". You can use them to process integer types (one of the main types) separately ).
The Left shift operator (<) can move the operator object on the left of the operator to the specified number of digits on the right of the operator (0 in the low position ).
The "Signed" right shift operator (>) moves the operator object on the left of the operator to the right of the specified number of digits on the right of the operator.
The "Signed" right shift operator uses the "symbol extension": If the value is positive, 0 is inserted at the high position; if the value is negative, 1 is inserted at the high position.
Java also adds an "unsigned" right shift operator (>>>), which uses "Zero extension": no matter whether it is positive or negative, 0 is inserted at a high position.
This operator is not available in C or C. If char, byte, or short is displaced, they are automatically converted to an int before the shift.
Only the five low positions on the right can be used. This prevents us from moving the unrealistic digits in an int number.
If a long value is processed, the final result is long. At this time, only the six low positions on the right will be used to prevent the long value from being moved beyond the existing ones.
However, a problem may also occur during "unsigned" right shift. If you perform the right shift operation on the byte or short value,
The results may not be correct (Java 1.0 and Java 1.1 are particularly prominent ). They are automatically converted to the int type and shifted to the right.
But "Zero extension" does not happen, so in those cases, the result of-1 will be obtained.

For example:
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 result:
4194303
18014398509481983
-1
-1

========================================================== ========================================================
I have to mention that it is also a very implicit point, that is, I found it on the blog of Einstein. He said it was the question on scjp,
The excerpt from his article is as follows:

The questions in scjp are really "awesome". Many of them are unexpected and interesting. Haha, the last one today is coming soon. It's too late,
Tomorrow I will 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, the output should be: 0, because the int type of Java occupies 4 bytes, that is, 32 bits. When 32 bits are shifted to the right, all bits should be changed to 0, but the output result is:-1,
After thinking about it for a long time, I posted a post online and asked, thank you very much for coffer283 and danieljill.
In the original shift operation in Java, because int represents 32 bits and the number of shifts is 32 modulo, When I >>> 32 is equal to I >>> 0, it is equivalent to not performing shift.
I tried the shift of the Long TYPE again. Long occupies 8 bytes, that is, 64 bits, so the shift count is 64.
Bytes ---------------------------------------------------------------------------------------------------

The above is his problem, which inspired me a lot and gave me a deep understanding of Java's shift operation.
At the same time, I also conducted an experiment on the shift period of the byte and short types, which is also 32, which is the same as that of the int type. In this way, I also verified the right shift operation of the byte and short types, it is automatically converted to the int type. I have verified that the three shift operators <, >>,> follow the shift period.
========================================================== ========================================================== ========

Well, I have finally finished my work and have a comprehensive understanding of Java shift operations. I don't feel like I 've spent the last few hours sorting out, Hoho ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.