Java Displacement Calculation clever Method

Source: Internet
Author: User

Left shift operation: x <n

X can be byte, short, Char, Int, and long basic types. N (displacement) can only be int type.

Compiler execution steps:

1) if X is of the byte, short, and Char Types, It is upgraded to int;

2) If X is of the byte, short, Char, int type, n is re-assigned (the process is to take the lower 5 digits of the complement code of N and convert it to the int value in decimal format, n: N = n % 32 );

If X is of the long type, n is re-assigned (the process is to take the low 6 digits of N's complement code and then convert it to the int value in decimal format, equivalent to the 64 Modulo for N: N = n % 64 );

(Because the int type is 4 bytes, that is, 32 bits, moving 32 bits does not make sense. For long, it is a modulo 64)

3) For X to shift n digits left, the entire expression generates a new value (the value of X remains unchanged );

<Is the left shift symbol, column x <1, that is, the content of X is shifted to the left (the content of X is not changed)

>>> It is a right shift symbol with a symbol bit. x> 1 means that the content of X is shifted to the right by one. If the content starts with 1, it is supplemented with 1. If it starts with 0, it is supplemented with 0, (the content of X does not change ).

>>> It is a right shift without a symbol, and X >>> 1 means that the content of X is shifted to the right, and 0 is added at the beginning (the content of X is not changed)

Note:

Java code

 
 
  1. // Move left: move left, and add 0 to the right
  2. For (INT I = 0; I <8; I ++)
  3. System. Out. Print (1 <I) + "");

Output

1 2 4 8 16 32 64 128

 
 
  1. // Shift right: Move to the right. If the sign bit (32 characters in int type) is 0, the sign bit is 1 on the left, and the sign bit is 1 on the left
  2. // Shifts the right of the 1 Symbol
  3. For (INT I = 0; I <8; I ++)
  4. System. Out. Print (integer. tohexstring (0x40000000> I) + "");

Output

40000000 20000000 10000000 8000000 4000000 2000000 1000000

 
 
  1. // Shifts the right of the 1 Symbol
  2. // The maximum value of 4 bits is 1000, and the value ranges from 1 to 1100, that is, C,
  3. For (INT I = 0; I <8; I ++)
  4. System. Out. Print (integer. tohexstring (0x80000000> I) + "");

Output

80000000 c0000000 e0000000 f0000000 f8000000 fc000000 fe000000 ff000000

There is no error in the general rule above, but there is a limit that for int type, the number of shifts cannot exceed 32, for long type, the number of shifts cannot exceed 64. Perform the following tests:

Java code

 
 
  1. System.out.println(Integer.toHexString(0x80000000 >> 31));   
  2. // output: ffffffff   
  3. System.out.println(Integer.toHexString(0x80000000 >> 32));   
  4. // output: 80000000 

0x80000000 after 31 bits are shifted to the right, each bit becomes 1 (that is,-1). According to this idea, the 32 bits are shifted to-1, but after 32 bits are shifted to the right, the result is the number itself.

After testing the data of the int and long types, we found that:

Java for the shift operation "A <|> B", first perform the B mod 32 | 64 operation, if A is int type, take mod 32, if a is of the double type, MOD 64 is used, and then the general shift operation rule mentioned above is used for the shift.

Here, we can understand why the bitset class is

 
 
  1. 1L << bitIndex 

This statement, because the programer familiar with JDK knows that writing 1l <(bitindex % 64) is redundant for JDK.

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.