Java bitwise operators

Java Bitwise operators

Java Bitwise operators are the ones that can easily be applied to the integer values, long, short, byte, and char values. They help in the manipulation of individual bits of an integer. In java, the user has 4 bitwise and 3-bit shift operators to perform bitwise operations. There are certain specific cases where these operators and shifts can be used; otherwise, the user cannot use them commonly.

Types of Java bitwise operators:

1.) Bitwise OR:

It is a binary operator and returns bit by bit values of OR operation. If any of the inputs is 1, the result will be 1, otherwise it will be 0. It is denoted by β€˜|’ sign which compares the two inputs.

OR( 0 | 1);  = 1

OR( 1 | 0);  = 1

OR( 0 | 0);  = 0

OR( 1 | 1);  = 1

Let us take 2 integers x and y.
x = 2;
y= 5;

Binary of 2= 0010;
Binary of 5= 0101;

0010
|0101
The output will be 0111

2.) Bitwise AND:

It is a binary operator and returns bit by bit values of AND operations. We denote it by β€˜&’ sign which compares the two inputs. If any of the inputs is 0, the result will be 0, otherwise, it will be 1.

What is the difference between & and &&?

& in java is a type of bitwise operator which helps in comparison of each input. It will evaluate both sides of an input.

However, && in Java is a type of logical operator which helps in the comparison of boolean values. It will first evaluate the left side of the condition. if it is satisfied, it will move to the right side.

AND( 0 | 1);  = 0

AND( 1 | 0);  = 0

AND( 0 | 0);  = 0

AND( 1 | 1);  = 1

Let us take 2 integers x and y.
x = 2;
y= 5;

Binary of 2= 0010;
Binary of 5= 0101;

0010
&0101
The output will be 0000

3.) Bitwise Compliment

It is a unary operator and it performs operations on only 1 input. We denote it using β€˜~’ sign. It seems to invert the patterns of the bits which mean it will change all the 0 inputs to 1. Similarly, all the 1 inputs to 0.  

~( 0);  = 1

~( 1);  = 0

Let us take an integer x.
x = 2;

Binary of 2= 0010;

~0010

The output will be 1101

4.) Bitwise XOR

It is a binary operator and returns bit by bit values of XOR operations. we denote it by β€˜^’ sign which compares the two inputs. It gives the output as 1 if both the inputs are same. Otherwise, the returns 0 when the inputs do not match.

XOR( 0 | 1);  = 0

XOR ( 1 | 0);  = 0

XOR( 0 | 0);  = 1

XOR( 1 | 1);  = 1

Let us take 2 integers x and y.
x = 2;
y= 5;

Binary of 2= 0010;
Binary of 5= 0101;

0010
^0101

The output will be 1000

We also have bitwise shift operators. They help in shifting the bits of numbers to left or right. we can perform this by either multiplying or dividing the number by 2. The types of shift operators in java are:

5.) Bitwise Right Shift operator

The operator shifts the bit to right and fills the void with 0 in the output. We denote it by β€˜>>’. The sign of the number decides the left bit. It is done by dividing the number with a power of 2.

6.) Bitwise Left Shift operator

The operator shifts the bit to left and fills the void with 0 in the output. It is denoted by β€˜<<’. The sign of the number decides the right bit. It is done by multiplying the number with a power of 2.

Where are Bitwise Operators Use?

The bitwise operators are mostly used in places where manipulation of individual bits is required.  They support any integer type and users can use them for the operations such as update a query. They are also used in the operations of the indexed tree.

Java Program to explain Bitwise Operators

public class DeveloperHelps { 
public static void main(String[] args) 
{ 
int a = 2; 
int b = 6; 
System.out.println("a&b = " + (a & b)); 
System.out.println("a|b = " + (a | b)); 
System.out.println("a^b = " + (a ^ b)); 
System.out.println("~a = " + ~a); 
a &= b; 
System.out.println("a= " + a); 
} 
}

The output of the above java program will be:

a&b = 2
a|b = 6
a^b = 4
~a = -3
a= 2

Leave a comment

Your email address will not be published.