Demo and summary of shift operator in Java (recommended)

First, there are three kinds of shift operators, and their operation types are only supported: byte / short / char / int and long.

< < move left operator means to move the binary data of the left operand by * bits to the left. After moving, the empty bits are filled with 0, and the redundant bits are discarded. (equivalent to multiplying 2 to the nth power)

>>Shift right operator, binary data moves * bits to the right, and how many bits are erased after its binary data? (it's not bad here, but my personal understanding is like this) (equivalent to dividing 2 to the nth power)

>>>For the unsigned shift right operator, no matter whether the highest bit before the shift is 0 or 1, the empty bit generated on the left after the shift right is filled with 0.

Let's use a demo to help understand:

View the running results, as shown in the figure:

First, the binary data of 16 is 10000;

Shift left by two digits, 10000 becomes 1000000;

Shift two bits to the right, 10000 becomes 100;

After converting the obtained binary data into ordinary data,

A the value after moving left is 64, i.e. the second power of 16 * 2 (16 * 4).

A the value after moving right is 4, i.e. the second power of 16 / 2 (16 / 4).

Is it much easier to understand the shift operator after reading this demo?

The above demo and summary (recommendation) on shift operators in Java is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>