learn more...The following table shows the standard bitwise operators in Java and a description of them.
To illustrate the function of these bitwise operators, we can use two byte values, A and B, which in java could be represented by a variable of type byte. The following table shows the binary notation of A and B (as there are 8 bits in a byte).
The AND (&) operator tests two bits and returns the resulting bit true if both test bits are true; otherwise, the return bit is false. The following table shows the result of A AND B.
The OR (|) operator tests two bits and returns the resulting bit true if any or both of the test bits are true; if they are both false, the return bit is also false. The following table shows the result of A OR B.
The XOR (^) operator tests two bits and returns the resulting bit true if one, and only one, of the bits is true; otherwise, if the two values are equal, the return bit is false. The following table shows the result of A XOR B.
The NOT (~) operator will invert all of the bits, where ones becomes zeros and zeros become ones, and is therefore a unary operator used with only one operand, whereas the other bitwise operators we have just seen were tested against two operands (binary operators), A and B. The following table shows the result of a NOT operation on byte A.
The bitwise AND, OR, and XOR operators can also be used with boolean expressions, as Boolean values effectively only contain one bit that is either true or false. This can be implemented in Java as follows: boolean musicOn = true; boolean televisionOn = true; boolean areBothOn = musicOn & televisionOn; // true boolean areAnyOn = musicOn | televisionOn; // true boolean isOnlyOneOn = musicOn ^ televisionOn; // false There are also assignment operators for these three bitwise operators, as shown in the following table.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Disclaimer
1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. link to this article |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||