Bit operator of Java

Integer to binary: integer toBInaryString(6)

< <: move left, 3 < < 2 = 3 * 2 * 2 = 12

>>: shift right, 3 > > 1 = 3 / 2 = 1 (for signed right shift, it depends on whether the first place is 0 or 1. If it is 0, it will supplement 0, and if it is 1, it will supplement 1; if it is unsigned right shift, it will only supplement 0)

>>>: unsigned shift right, 3 > > > 1 = 3 / 1 = 1, Note that there is no unsigned left shift

&: and operation, 6 & 3 = 2 (when the corresponding position of two numbers in binary is 1, the result is 1, for example, 1010 & 1101 = 1000)

|: or operation, 6 | 3 = 7 (when the corresponding positions of two numbers in binary are 0, the result is 0, for example, 10100 & 11010 = 11110)

^: XOR operation, 6 ^ 3 = 5 (the result is 0 only when the corresponding positions of two numbers in binary are 0 or 1, for example, 10100 & 11010 = 01110)

~: inverse code, ~ 6 = - 7 (the original code, inverse code and complement of a positive number are its own, the inverse code of a negative number is the inverse of the division sign bit of the original code, and the complement is the inverse code + 1)

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