Types of Java Shift operations
Basics: We know that in Java, the int type occupies 32 bits, can represent a positive number, or it can represent a negative number. A positive number is converted to binary with a maximum of 0, and the binary of negative numbers is up to 1
Example:
-5 converted to binary after: 1111 1111 1111 1111 1111 1111 1111 1011
How is the binary of negative numbers converted?
Negative numbers are converted to binary, that is, each bit of the complement of its opposite number (positive) is reversed (1 to 0,0 to 1) and the final value is added 1, which completes the complement operation of the negative. This becomes the binary.
5 converted to binary after: 0000 0000 0000 0000 0000 0000 0000 0000 0101
- Shift Right >>
Positive shift right, high 0
Example: 5>>3-->0000 0000 0000 0000 0000 0000 0000 0000 0000-->0
Negative number right shift, high fill 1
Example: -5>>3-->1111 1111 1111 1111 1111 1111 1111 1111-->-1
- Unsigned Right Shift >>>
Unsigned right shift, high fill 0
Example: -5>>>3-->0001 1111 1111 1111 1111 1111 1111 1111-->536870911
- Positive negative shift left <<
Positive negative shift left is low 0
- Bit XOR or
Bitwise XOR: The nth position of the first operand is the opposite of the nth bit of the second operand, then the nth of the result is also 1, otherwise 0
Java SHIFT and XOR operations