Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Bitwise Operators

Bitwise AND operator (&):

The symbol for this operator is &. This operator performs AND operation on the individual bits of the numbers. This operator produces 1 if both operands are 1, otherwise it will produce 0. To understand AND operator, see the truth table

sl.no   X   Y   X&Y
1   0   0   0
2   0   1   0
3   1   0   0
4   1   1   1

Example:
//if
int x=5, y=6, //then find the value of x&y.
X=5= 00000101
Y=6=00000110
x&y=00000100
// now
x&y = 00000100.// This is nothing but 4 (in decimal).
Bitwise OR operator (|):

This operator is presented by the pipe symbol (|). This operator performs OR operation on the bits of the numbers. This operator produces 0, if both operands are 0, otherwise it produces 1. See the truth table

sl.no   X   Y   X|Y
1   0   0   0
2   0   1   1
3   1   0   1
4   1   1   1

// let us see the following example
x= 5= 00000101
y= 6= 00000110
x|y= 00000111
now x|y= 00000111 //which is nothing but 7 in decimal.
Bitwise XOR operator (^):

This operator is represented by the symbol (^). This operator performs exclusiveOR operation on the bits of the numbers. To understand the XOR operation see the following truth table

sl.no   X   Y   X^Y
1   0   0   0
2   0   1   1
3   1   0   1
4   1   1   0

Example
X=5= 00000101
Y=6= 00000110
X^y= 00000011
x^y=00000011 // which is nothing but 3 in decimal.

Bitwise left shift operator (<<):

This operator is used for shifting the bits left. It requires two operands. The left operand is the operand whose bits are shifted and right operand indicates the number of bits to be shifted. On shifting the bits left, an equal number of bit position on the right are vacated. These positions are filled in with 0 bits. For example, let a=5, consider the statement

y=a<<3

The value in the integer a is shifted to the left by three bit positions. The result is assigned to y. Since the value of a is 0000 0000 0000 0101 after the execution the value of y will be 0000 0000 0010 1000 which is nothing but 40 in decimal

left shift <<
Drop Of
0000 0000 0000 0101 //insert 0's
//After left bit shift by 3 places i.e. x<<3
0000 0000 0010 1000

The effect of shifting a variable to the left by one bit position is to multiply it by 2. In this example, x is shifted left by 3 positions, which is equivalent to multiplying x by 2*2*2 . since the initial value of x is 5, the value assigned to y is 5*8=40.

Program:
class LeftshiftDemo
{
	public static void main(String args[])
	{
		int x=5,y;
		y=x<<3;
		System.out.println("y="+y);
	}
}
Output

y=40

Bitwise Right shift operator (>>):

This operator is similar to the left shift operator, except that it shifts the bits to the right side. On shifting the bits right , an equal number of bit position on the left are vacted. Consider the following example.

int x=5
Y= x>>2;
//The value of x is shifted to the right by 2 positions. Since the value of x is
0000 0000 0000 0101,
//the value of y after the execution will be
0000 0000 0000 0001 //(1 in decimal).

Hence the shifting to right by 2bit "0000 0000 0000 0001" =4. Since the initial value of x is 5, shifting to the right by 2bit positions will give the value 1 (5/4=1).

Example:
class RightShiftDemo
{
	public static void main(String args[])
	{
		int x=5,y;
		y=x>>3;
		System.out.println("y="+y);
	}
}

Output:

y=0;

Bitwise Zero fills Right Shift Operator (>>>):

This operator also shifts the bits of the number towards right a specified number of positions. But it stores 0 in the sign bit. The symbol for this operator is >>>. Since it always fills 0 in the sign bit, it is called Zero fill right shift operator. If we apply >>> on a positive number, it gives the same output as that of >>. But in case of negative number, the output will be positive since the sign bit is replaced by a 0.

Program:
class BitwiseZeroFillDemo
{
	public static void main(String args[])
	{
		int x=5,y;
		y=x>>>2;
		System.out.println("y="+y);
	}
}

Output

y=1

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext