Java Shift Supplement __java

Source: Internet
Author: User

One
int value = Integer.parseint ("00001111101001011111000010101100″,2);//That value=262533292

Perform a <<32 bit operation on the int variable value, that is, value <<=32, how much is this result?
If you think the answer is 0, then it's wrong, and it turns out to be 262533292.
Do not doubt that the Java int is 32-bit problem, but you do not understand the Java << operations.
The following rules are followed in Java regardless of whether you move left or right

Value<<n (where value is int,n>=0) is equivalent to value<< (N%32)
Value>>n (where value is int,n>=0) is equivalent to value>> (N%32)
Value>>>n (where value is int,n>=0) is equivalent to value>>> (N%32)

For a long type:
Value<<n (where value is long,n>=0) is equivalent to value<< (n%64)
Value>>n (where value is long,n>=0) is equivalent to value>> (n%64)
Value>>>n (where value is long,n>=0) is equivalent to value>>> (n%64)

And for Byte, short, char to follow the rules of int

Two
int value = Integer.parseint ("00001111101001011111000010101100″,2);//That value=262533292
Value <<=-38, how much is this result?
You might think it compiles, or do you think it's moving from left to right?
However, the fact is not as simple as it is supposed to be, and the result is-1342177280, how it came out. Listen to me in one by one ways.
If the value of int is shifted to a negative digit, Java will intercept the lower 5 digits of that negative number.
-38 binary representation is: 11111111111111111111111111011010, intercept low 5:11010, this number is 26, that is to say:
Value <<=-38 is equivalent to value <<= 26, the resulting binary is 10110000000000000000000000000000, the first is 1,
So the binary is the complement, replaced by its absolute value of the source code is 01010000000000000000000000000000, corresponding to the decimal 1342177280,
Plus minus sign is-1342177280.
For the int is to take a low 5 bit, for long is to take a low 6 bit
Other words:
Value <<-N (value int,n>=0) is equivalent to value << (-N & 31)
Value >>-N (value int,n>=0) is equivalent to value >> (-N & 31)
Value >>>-N (value int,n>=0) is equivalent to value >>> (-N &) value << (n%32+32)

&NBSP
  for long
   value <<-N (value long,n>=0) is equivalent to value << (-N &)
   value >>-N (value long,n>=0) is equivalent to value >> (-N &)
   value >>>-n (value is long,n>=0) is equivalent to value >>> (-N &)  value << (n%64+64)
  
  and for by TE, short, and char follow the rules of Int.

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.