Introduction to Java increment and decrement operators and shift operators

Excerpted from JavaGuide ("Java Learning + Interview Guide" covers the core knowledge that most Java programmers need to master. To prepare for Java interviews, JavaGuide is the first choice!)

Increment and decrement operators

In the process of writing code, a common situation is that an integer type variable needs to be increased by 1 or decreased by 1. Java provides a special operator for this kind of expression, called the self-increment operator (++) and the self-decrement operator (-).

The ++ and – operators can be placed before or after the variable. When the operator is placed before the variable (prefix), it is incremented/decremented first, and then assigned; when the operator is placed after the variable (suffix), the value is assigned first, and then incremented/decremented. For example, at that b = ++atime , first self-increment (self-increment by 1), and then assign (assign to b); b = a++at that time, first assign a value (assign to b), and then self-increment (self-increment by 1). That is, ++a outputs the value of a+1, and a++ outputs the value of a. A mantra is: "Add/subtract the symbol first, add/subtract the symbol after the last".

shift operator

The shift operator is one of the most basic operators and is included in almost every programming language. In the shift operation, the operated data is regarded as a binary number, and the shift is the operation of shifting it to the left or right by a certain number of bits.

The shift operator is widely used in various frameworks and JDK's own source code. The source code of the method HashMapin (JDK1.8) hashuses the shift operator:

static final int hash(Object key) {
    
    
    int h;
    // key.hashCode():返回散列值也就是hashcode
    // ^ :按位异或
    // >>>:无符号右移,忽略符号位,空位都以0补齐
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
  }

Used in Java code <<, >>and >>>converted scripts will run more efficiently.

It is still necessary to master the most basic knowledge of shift operators, which can not only help us use them in the code, but also help us understand the codes involving shift operators in the source code.

There are three shift operators in Java:

  • <<: Left shift operator, shifts a number of bits to the left, discards high bits, and pads low bits with zeros. x << 1, which is equivalent to multiplying x by 2 (without overflow).
  • >>: Shift right with a sign, shift right by several bits, complement the sign bit with the high bit, and discard the low bit. The positive number is filled with 0, and the negative number is filled with 1. x >> 1, which is equivalent to dividing x by 2.
  • >>>: Unsigned right shift, ignoring the sign bit, filling the empty bits with 0.

Since the performance in binary is quite special, it cannot be used for shift operations double.float

The only types supported by shift operators are intand long, and the compiler will convert them to types before performing shifts on short, , byteand types .charint

What happens if the number of bits shifted exceeds the number of bits occupied by the value?

When the number of left/right shifts of the int type is greater than or equal to 32 bits, the remainder (%) will be calculated before the left/right shift operation. That is to say, shifting left/right by 32 bits is equivalent to no shift operation (32%32=0), and shifting left/right by 42 bits is equivalent to shifting left/right by 10 bits (42%32=10). When the long type performs left/right shift operations, since the binary corresponding to long is 64 bits, the base of the remainder operation also becomes 64.

That is: x<<42equal to x<<10, x>>42equal to x>>10, x >>>42equal to x >>> 10.

Left shift operator code example :

int i = -1;
System.out.println("初始数据: " + i);
System.out.println("初始数据对应的二进制字符串: " + Integer.toBinaryString(i));
i <<= 10;
System.out.println("左移 10 位后的数据 " + i);
System.out.println("左移 10 位后的数据对应的二进制字符 " + Integer.toBinaryString(i));

output:

初始数据: -1
初始数据对应的二进制字符串: 11111111111111111111111111111111
左移 10 位后的数据 -1024
左移 10 位后的数据对应的二进制字符 11111111111111111111110000000000

Since the left shift number is greater than or equal to 32 bits, the remainder (%) will be calculated before the left shift operation, so the following code shifts left by 42 bits is equivalent to shifting left by 10 bits (42%32=10), and the output result is the same as the previous code.

int i = -1;
System.out.println("初始数据: " + i);
System.out.println("初始数据对应的二进制字符串: " + Integer.toBinaryString(i));
i <<= 42;
System.out.println("左移 10 位后的数据 " + i);
System.out.println("左移 10 位后的数据对应的二进制字符 " + Integer.toBinaryString(i));

The use of the right shift operator is similar, and there is a space issue, so I won’t demonstrate it here.

Guess you like

Origin blog.csdn.net/qq_34337272/article/details/130010981