Bitwise Operators in Java

The bitwise operators, & (bitwise and), | (bitwise or), ^ (bitwise exclusive or), and ~ (bitwise complement) are used to manipulate the bits of the Java integral types. The operators act similarly to the equivalent logical operators, except that operations apply to individual bits.

The & operator sets the resulting bit to 1 if the corresponding bit in both operands is 1; otherwise, the resulting bit is 0. The | operator sets the resulting bit to 1 if either bit is 1; otherwise, the resulting bit is 0. The ^ operator sets the resulting bit to 1 if the two bits are different; otherwise, the resulting bit is 0. Table 3.2 shows the results of these bitwise binary operations applied to operands a and b, where a is the byte representation of the number 3, and b is the byte representation of the number 2.

No comments:

Post a Comment