java left shift << and right shift>>

For positive integers

<< shift left

Move all the numbers to the left by the corresponding number of digits in binary form, and fill in the low-order space with zero.

>> shift right

According to the binary form, move all the numbers to the right by the corresponding number of shift bits, the low bit is shifted out (discarded), and the highest bit is shifted to the original high bit value.

For example: such as: a = 00110111, then a>>2=00001101, b=11010011, then b>>2 = 11110100

 

    public static void main(String[] args) {
        int a=53;
        System.out.println("53右移2位"+(a>>2));   // 相当于53/(2*2)
        System.out.println("53左移1位"+(a<<1));   // 相当于53*(2)

    }

result:

Guess you like

Origin blog.csdn.net/Growing_hacker/article/details/108529775