Bitwise operators are used to process numbers with respect to their binary representation. The main task of bitwise operators is to manipulate binary values bitwise.
The operations are directly supported by the processor. Bitwise operations are required for working with device drivers, low-level graphics, cryptography, and network communication.
Operator | Description | Priority |
---|---|---|
~ | Bit complement | 1 |
<< | Left shift of bit patterns | 4 |
>> | Right shift of bit patterns | 4 |
>>> | Right shift of bit patterns (with tracing zeros) | 4 |
& | Bitwise AND | 7 |
^ | Bitwise XOR | 8 |
| | Bitwise OR | 9 |
The code defines a class named “Bitoperators” that demonstrates the use of bitwise operators in Java. The main method of the class performs various bitwise operations on the variables ‘a’, ‘b’ and ‘c’.
Here is a brief explanation of the code:
Overall, the code demonstrates how to use bitwise operators in Java and the results of performing bitwise operations on binary numbers.
public class Bitoperators {
public static void main(String[] args) {
int x, y, a = 7, b = 12, c = 15;
x = a & b;
System.out.println("x = " + x); // a 0111 = 7
// b 1100 = 12
// a & b 0100 = 4 --> x = 4
y = a ^ c;
System.out.println("y = " + y); // a 0111 = 7
// c 1111 = 15
// a ^ c 1000 = 8 --> y = 8
}
}
x = 4
y = 8