>> num;", where value is the binary number to be moved, num is the number of digits to be moved, and result is the operation result."/> >> num;", where value is the binary number to be moved, num is the number of digits to be moved, and result is the operation result.">
Home >Java >javaTutorial >How to use unsigned right shift in java
The usage of unsigned right shift in Java is to move a binary number to the right by the specified number of digits and fill it with zero bits on the left, regardless of the sign bit of the original number. The syntax of the unsigned right shift operator is "int result = value >>> num;", where value is the binary number to be moved, num is the number of digits to be moved, and result is the operation result.
In Java, the unsigned right shift operator (>>>) is used to shift a binary number to the right by a specified number of digits. And zero bits are padded on the left, regardless of the sign bit of the original number. The syntax of the unsigned right shift operator is as follows:
int result = value >>> num;
Among them, value is the binary number to be moved, num is the number of digits to be moved, and result is the result of the operation.
The sample code is as follows:
int num = -10; // 要移动的二进制数 int shift = 2; // 要移动的位数 int result = num >>> shift; // 无符号右移运算符的计算结果 System.out.println(result); // 输出结果为1073741821
In this example, the binary number -10 is unsigned and right-shifted by 2 bits to get 0011111111111111111111111111110, and then converted to a decimal number 1073741821, the final output result is 1073741821.
It should be noted that the unsigned right shift operator can only be used for unsigned integer types (such as int, long, etc.) and cannot be used for signed integer types (such as byte, short, etc.). In addition, the number of bits to be moved must be a non-negative integer, otherwise an exception will be thrown. When using the unsigned right shift operator, special attention should be paid to how zero bits are padded to avoid unexpected results.
The above is the detailed content of How to use unsigned right shift in java. For more information, please follow other related articles on the PHP Chinese website!