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 Example

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:

  1. The variables ‘x’ and ‘y’ are declared as integers, and the variables ‘a’, ‘b’ and ‘c’ are given integer values of 7, 12 and 15 respectively.
  2. The first bitwise operation is the “bitwise AND” operator (&) between the variables ‘a’ and ‘b’. The result of the operation is stored in the variable ‘x’.
  3. The second bitwise operation is the “bitwise exclusive OR” operator (^) between the variables ‘a’ and ‘c’. The result of the operation is stored in the variable ‘y’.
  4. The values of ‘x’ and ‘y’ are then printed to the console, demonstrating the results of the bitwise operations.

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
    }
}
Output
x = 4
y = 8