[JAVA] Why is there no unsigned right shift operator in C / C ++?

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".

Introduction

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?

Java has no unsigned numeric type

According to Java § 4.2.1, Java's built-in integer types are There are the following four.

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 intandlong`, it can be said that Java does not have an unsigned integer type when it comes to shift operations.

The need for unsigned shifts

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 aString. 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.

Why there is no right shift operator in C / C ++

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.

end

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

Why is there no unsigned right shift operator in C / C ++?
Is there no type in Ruby?
In 2021, there is no topic in Java these days (Poem)
When there is no output to stdout in docker log
Initial value when there is no form object property in Spring request
gradle's mybatis-generator-plugin says there is no MapperAnnotationPlugin
[Deep Learning from scratch] 2. There is no such thing as NumPy in Java.