Explain bitwise operators in Java
The bit operator of Java is used to operate on a single "bit" in the basic data type of an integer, that is, the hexadecimal bit. We know that bits are 0 and 1, so bit operation is the basic operation of these data. If a value whose basic type is char, byte or short is shifted, it will be converted to int and then shifted
Bitwise operators in Java
The bitwise operator performs Boolean algebraic operations on the bits corresponding to the two parameters and finally generates a result. This operator has and (&), not (~), or (|), XOR (^). We know that the unit "bit" (bit), that is, the hexadecimal bits, are both 0 and 1. The XOR (^) may be more complex. In the bits of the two operands, if they are the same, the result is 0, and if they are different, the result is 1. Then the most basic logic is like this.
1 & 1 → 11 & 0 → 0 ~ 1 → 0 ~ 0 → 11|1 → 11|0 → 11 ^ 0 → 1 (1 is 01, 0 is 00, then the result is 01, or 1) 1 ^ 1 → 0
Java shift operator
Java's shift operator is nothing more than shifting binary.
Moving < < is to move left, that is, all binaries move one bit to the left, 0010 0000 < < 1 is equal to 0100 0000
Move > > to the right, that is, all binaries move one bit to the right. 0010 0000 > > 1 is equal to 0001 0000.
Try the following example. The integer type is also converted into binary for calculation:
Of course, there are also ternary operators in Java. The functions of this part are somewhat similar to if else. See more details:
Ternary operators in Java