Programs

What is Bitwise Operator In Java? and It’s Classification (With Examples)

Java is one of the most predominant programming languages in India, commanding around 20 percent of the market share. A bitwise operator in Java is a symbol/notation that performs a specified operation on standalone bits, taken one at a time. It is used to manipulate individual bits of a binary number and can be used with a variety of integer types – char, int, long, short, byte.

A bitwise operator in Java can be broadly classified into 7 types: 

Bitwise Operator in Java Symbol Description
Bitwise AND | Preserves a bit in the result if and only if it exists in both operands.
Bitwise OR & Preserves a bit if it exists in either of the two operands.
Bitwise XOR ^ Preserves a bit if it is present only in one operand but not both.
Bitwise COMPLEMENT ~ Also known as 1’s complement. Just reverse the bits of the operand.
Bitwise LEFT SHIFT << The bits in the left operand are shifted to the left by the number of places specified in the right operand.
SIGNED RIGHT SHIFT >> The bits in the left operand are shifted to the right by the number of places specified in the right operand.
UNSIGNED RIGHT SHIFT >>> Exactly like SIGNED RIGHT SHIFT plus shifted bits are filled with zero.

Let us now explore in detail each bitwise operator in Java:

1. OR (A | B)

Truth Table

A B Output
0 0 0
0 1 1
1 0 1
1 1 1

Example

A = 1011 1110

B = 0011 0101

A | B = 1011 1111

Syntax

class Main {

  public static void main(String[] args) {

    int operand1 = 190, operand2 = 53, output;

    // bitwise OR between 190 and 53

    output = operand1 | operand2;

    System.out.println(output);    // program prints 191

  }

}

2. AND (A & B)

Truth Table

A B Output
0 0 0
0 1 0
1 0 0
1 1 1

Example

A = 1011 1110

B = 0011 0101

A & B = 0011 0100

Syntax

class Main {

  public static void main(String[] args) {

    int operand1 = 190, operand2 = 53, output;

    // bitwise OR between 190 and 53

    output = operand1 | operand2;

    System.out.println(output);    // program prints 52

  }

}

  1. INCLUSIVE OR / XOR (A ^ B)

Truth Table

A B Output
0 0 0
0 1 1
1 0 1
1 1 0

Example

A = 1011 1110

B = 0011 0101

A ^ B = 1000 0011

Syntax

class Main {

  public static void main(String[] args) {

    int operand1 = 190, operand2 = 53, output;

    // bitwise OR between 190 and 53

    output = operand1 | operand2;

    System.out.println(output);    // program prints 131

  }

}

3. COMPLEMENT (A ~ B)

In decimal terms, the bitwise complement of a number A is – (A+1). However, in the binary domain, we need to first calculate its 1’s complement and then convert it into its 2’s complement to get the desired output.

1’s Complement – Just reverse the bits. For example:

A = 1011 1110 (190)

1’s Complement of A = 0100 0001

2’s Complement of A = 1’s Complement of A + 1 = 0100 0010

Syntax

class Main {

  public static void main(String[] args) {

    int operand4 = 190, output;

    // bitwise complement of 190

    output = ~operand4;

    System.out.println(output);    // prints -191

  }

}

  1. LEFT SHIFT (A << 1)

Suppose A = 1011 1110

A << 1 = ?

This bitwise operator in Java makes all the bits shift to the left by 1, i.e., the output is 0111 1100. The left-most bit (most significant bit or MSB) is discarded, and a 0 is added to the rightmost empty place (least significant bit or LSB). This is done for every vacant place at the right.

Syntax

class Main {

  public static void main(String[] args) {

    int operand = 3; // binary – 11 

    // 1-bit left shift operation 

    int output= operand << 1;

    System.out.println(output);    // prints 2

  }

}

4. SIGNED RIGHT SHIFT (A >>2)

This bitwise operator in Java shifts the bit pattern to the right by the specified number of places. LSBs are discarded, and the MSBs are filled with the sign bit. 0 represents a positive sign, while 1 represents a negative sign.

Syntax

class Main {

  public static void main(String[] args) {

    int operand 1 = 2;

    int operand2 = -2;  

    // 2 bit signed right shift

    System.out.println(operand1 >> 2);    // prints 1

    System.out.println(operand2 >> 2);    // prints -1

  }

}

5. UNSIGNED RIGHT SHIFT (A >>>2)

The operation here is exactly similar to SIGNED RIGHT SHIFT except that the vacant left-most bits are replaced with 0 instead of the sign bits.

Syntax

class Main {

  public static void main(String[] args) {  

    int operand  = 240; // binary number – 1111 0000    

    // 2 bit unsigned right shift

    System.out.println(operand >>> 2);    // prints 60

  }

}

Conclusion

Keen to master Java and make a career out of it? Check out upGrad’s Software & Technology track. Enrol for the 13-month Executive PG Program in Full-Stack Development and get your hands dirty with real-world projects. Affiliated with the reputed IIIT Bangalore, you get the opportunity to learn 16 programming languages & tools, and 3 job interviews guaranteed. 

 

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
LEARN MORE

0 replies on “What is Bitwise Operator In Java? and It’s Classification (With Examples)”

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×