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 |
Line | Description |
---|---|
3 | Declares the integer variables x, y, a, b, c .x and y are not initialized.The variable a is initialized with the value 7,b with the value 12 andc with the value 15. |
4 | Bitwise AND linking of the two variables a and b |
5 | Outputs the bitwise AND linking |
8 | Bitwise XOR linking of the two variables a and c |
9 | Outputs the bitwise XOR linking |
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