Shizuoka University Information AC (IT) It is the 6th day. The day before (5th day) is vacant. The day before (4th day) is asdfg0280's "I tried using Nuxt.js".
When quoting the Java and JVM Specifications, write them as "Java §1.2.3" and "JVM §4.5.6", respectively. The Java / JVM version is 11.
Unsigned right shift operator? Java has an operator called the unsigned right shift operator.
int i = -4 >> 1; // -2 (signed right shift)
int j = -4 >>> 1; // 2147483646 (unsigned right shift)
While the normal shift saves the sign by sign extension (arithmetic shift), the unsigned right shift shifts as it is (logical shift) without worrying about the sign bit. (Java §15.19)
Why does Java have this operator, which C / C ++ does not have?
According to Java § 4.2.1, Java's built-in integer types are There are the following four.
byte
(-128 ~ 127)short
(-32768 ~ 32767)int
(-2147483648 ~ 2147483647)long
(-9223372036854775808 ~ 9223372036854775807)
-- char
('\ u0000'
~ '\ uffff'
, that is, 0 ~ 65535)As you can see from the range of values, everything except char
is signed. (JVM §2.3 also states that only char
is unsigned. )
Also, even char
is internally converted to ʻintduring the shift operation and then used in the operation. ("Unary numeric promotion", [Java §5.6.1](https://docs.oracle.com/javase/specs/jls/se11/html/jls-5.html#jls-5.6.1)) According to [JVM §2.11.1](https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-2.html#jvms-2.11.1), the shift operation instruction of the JVM is
Since it is defined only in intand
long`, it can be said that Java does not have an unsigned integer type when it comes to shift operations.
Now, what's the problem without the unsigned integer type? Take a look at the following code.
String toBits(int val) {
var builder = new StringBuilder();
for(int j = 0; j < Integer.SIZE; j++, val >>>= 1) {
builder.append((val & 1) == 1 ? "1" : "0");
}
return builder.reverse().toString();
}
This is a function that takes a ʻinttype argument and returns its bit representation as a
String. Example:
-1->
"11111111111111111111111111111111"
20181206->
"00000001001100111111000011010110"`
The third line uses >>> =
(an operator that assigns while >>>
.
Even if there is no >>>
in the Java world, this does not mean that you cannot write this function.
However, if the negative value is arithmetically shifted to the right, the high-order bit is filled with 1 in the sign extension, so special operations are required for negative numbers.
As you can see, in Java, the unsigned right shift operator doesn't die without it, but it turns out to be more convenient.
In C / C ++, there are unsigned integer types, and the standard stipulates that shifts for these types are logical shifts. Therefore, you can do the same without the right shift operator.
The next day (7th day) is vacant. The day after next (8th day) is hi97_ia16's "The story of developing a system that automatically acquires university grades with Node.js". (There are no people on either side ...)
Recommended Posts