Java moves left and fills zeros

I want to move left, but fill in zero, just like

int number = 20 >>> 10 = ( 0000 0000 0000 0000 0000 0000 0001 0100 ) 
int number = 20 >>> 32‎ =  ( 0000 0000 0000 0000 0000 0000 0000 0000 )

I want to do the same for the left shift, because there is no operator < < for the leftshift

int number = 20 << 32 = 0000 0000 0000 0000 0000 0000 0001 0100 ;

I want to fill it with zeros, like the > > > operator What should I do?

Solution

The shift left operator will introduce zeros according to your needs The reason why there are two right shift operators (> > > > > > >) is that in the complement form of 2, negative numbers have a bit value of 1 at the leftmost bit position The shift right operator > > will add sign bits (1 in the case of negative numbers and 0 in the case of positive numbers or zeros) to the left, while the other (> > >) will always add zeros

Both shift right operators have their own uses

The language specification states that if the bit shift operator is applied to int, because it is 32 bits long, only the lowest bit of 5 bits is used to determine the number of shifts

Therefore, if you move 32 in binary mode, that is, 100000, it is equivalent to moving 0, which means don't shift! If you want to shift 64 bits long, the lowest 6 bits are only used to indicate how many times to shift

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
分享
二维码
< <上一篇
下一篇>>