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
- // Move left: move left, and add 0 to the right
- For (INT I = 0; I <8; I ++)
- System. Out. Print (1 <I) + "");
Output
1 2 4 8 16 32 64 128
- // 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
- // Shifts the right of the 1 Symbol
- For (INT I = 0; I <8; I ++)
- System. Out. Print (integer. tohexstring (0x40000000> I) + "");
Output
40000000 20000000 10000000 8000000 4000000 2000000 1000000
- // Shifts the right of the 1 Symbol
- // The maximum value of 4 bits is 1000, and the value ranges from 1 to 1100, that is, C,
- For (INT I = 0; I <8; I ++)
- 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
- System.out.println(Integer.toHexString(0x80000000 >> 31));
- // output: ffffffff
- System.out.println(Integer.toHexString(0x80000000 >> 32));
- // 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
- 1L << bitIndex
This statement, because the programer familiar with JDK knows that writing 1l <(bitindex % 64) is redundant for JDK.