Java Basics: Shift Operators

The previous article covered conditional operators, and this article covers shift operators.

overview

In Java, there are the following shift operators:

operator input example explain
<< x << y Shifts the bits in x to the left by y bits, padding the right end with 0s.
>> x >> y Shifts the bits in x to the right by y bits, left padding with the same sign as the most significant bit at the time.
>>> x >>> y Shifts the bits in x to the right by y bits, padding the left with 0s.

Note: Shift operators are used on integer values.

<<

<< Shifts the bits of the left operand to the left by the bits of the right operand, shifting left means multiplying the original value by 2 to the shift power.

grammar:

value<<position
  • value represents the binary value on which the shift operation will be performed
  • position refers to the specified position where a left shift occurs by shifting bits to the left by that position

For example:

The binary number of the number 5 is 101, and shifting one bit to the left becomes 1010, which is 10.

Code example:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/5 11:27
 * @description:
 */
public class Nine {

    public static void main(String[] args) {

        // 数字是5
        int num = 5;

        // 向左移一位
        int result = num << 1;

        // 开始执行左移操作
        System.out.println("左移操作后:" + result);
    }
}

Results of the:

Let's explain it graphically:

>>

Contrary to <<, >> shifts the bits of the left operand to the right by the bits of the right operand. Right shifting means dividing the original value by the shift power of 2. The leftmost bit has the same sign as the most significant bit. The reason for inputting the same sign as the most significant bit is because the most significant bit of a negative number is 1 in 2’s complement representation, shifting the decimal number -4 with the highest bit of 1 to the right, and adding 0 to the left end of the operation result is incorrect.

For example:

The binary number of the number 32 is 100000, shifting two bits to the right becomes 1000, which is 8.

Code example:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/5 16:25
 * @description:
 */
public class Ten {

    public static void main(String[] args) {

        // 数字是32
        int num = 32;

        // 向右移两位
        int result = num >> 2;

        // 开始执行右移操作
        System.out.println("右移操作后:" + result);
    }
}

Results of the:

Let's explain it graphically:

The above demonstrates the case of a positive number. If it is a negative number, for example -32 >> 2, guess what the final result is?

the answer is:

>>>

Move the bit of the left operand to the right by the bit of the right operand. >>> There is no distinction between positive and negative in the operation result. The >>> operator is used for sign-insensitive operations, also known as "unsigned right shift operator".

In the unsigned right shift operator, there is no difference from >> in the case of positive numbers. In the case of negative numbers, 0 will be filled at the far left. Let's demonstrate it with code:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/5 16:25
 * @description:
 */
public class Ten {

    public static void main(String[] args) {

        // 数字是32
        int num1 = 32;

        // 数字是-32
        int num2 = -32;

        // 向右移两位
        int result1 = num1 >>> 2;
        int result2 = num2 >>> 2;

        // 开始执行右移操作
        System.out.println(num1 + "右移操作后:" + result1);
        System.out.println(num2 + "右移操作后:" + result2);
    }
}

Results of the:

It can be seen that positive 32 is easy to understand, so what does the -32 process look like?

According to my description above, let's draw a picture to explain:

According to the picture above, it should be easy to understand!

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132236308