Bitoperators

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.

OperatorDescriptionPriority
~Bit complement1
<<Left shift of bit patterns 4
>>Right shift of bit patterns 4
>>>Right shift of bit patterns (with tracing zeros)4
&Bitwise AND7
^Bitwise XOR8
|Bitwise OR9

Code Explanation

LineDescription
3Declares 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 and
c with the value 15.
4Bitwise AND linking of the two variables a and b
5Outputs the bitwise AND linking
8Bitwise XOR linking of the two variables a and c
9Outputs 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
    }
}
Output
x = 4
y = 8