Java2all.com  

Bitwise Operators





Share This Topic -

 

OPERATOR

MEANING

EXPLANATION

EXAMPLE

RESULT

~

Bitwise unary NOT

This sign is used for inverts all the bits

~42

213

&

Bitwise AND

Produce a 1 bit if both operands are also 1 otherwise 0

2 & 7

2

|

Bitwise OR

either of the bits in the operands is a 1,

then the resultant bit is a 1 otherwise 0

2 | 7

7

^

Bitwise exclusive OR

if exactly one operand is 1, then the result

is 1. Otherwise, the result is zero

2 ^ 7

5

>> 

Shift right

The right shift operator, >>, shifts all of the bits in a value to the right a specified number

of times.

7 >> 2

1

>>> 

Shift right zero fill

shift a zero into the high-order bit no matter

what its initial value was

-1 >>> 30

3

<< 

Shift left

The left shift operator, <<, shifts all of the bits in a value to the left a specified number

of times.

2 << 2

8

&=

Bitwise AND assignment

This is a short sign of AND operation on same variable

a=2

a& = 2

a = 2

|=

Bitwise OR assignment

This is a short sign of OR operation on same variable

a=2

a| = 7

a = 7

^=

Bitwise exclusive OR assignment

This is a short sign of XOR operation on same variable

a = 2

a^ = 7

a = 5

>>=

Shift right assignment

This is a short sign of shift right operation on same variable

a = 7

a>>  =  2

a = 1

>>>=

Shift right zero fill assignment

This is a short sign of shift right zero fill operation on same variable

a = -1

a>>> = 30

a = 3

<<=

Shift left assignment

This is a short sign of shift left operation on same variable

a = 2

a<< = 2

a = 8

 
EX.
 
 public class BitewiseDemo
{
	public static void main(String[] args) 
	{
		System.out.println("..............Bitewise operators.................");
		System.out.println("<-------Bitewise Logical Operators------->");
		String binary[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
						   "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
		int a = 2; // 0 + 0 + 2 + 0 or 0010 in binary
		int b = 7; // 0 + 4 + 2 + 1 or 0111 in binary
		int c = a | b;		//Bitwise AND operator
		int d = a & b;		//Bitwise OR operator
		int e = a ^ b;		//Bitwise XOR(exclusive OR) operator
		int f = ~a & a ; 	//Bitwise unary NOT operator, a = 0010 so ~a = 1101 hence f = ~a & a = 1101 & 0010 = 0000 
		int g = (~a & b) | (a & ~b); 
		System.out.println("The binary value of a = " + binary[a]);
		System.out.println("The binary value of b = " + binary[b]);
		System.out.println("The Bitwise OR : a | b = " + binary[c] + " and Decimal value = "+c);
		System.out.println("The Bitwise AND : a & b = " + binary[d] + " and Decimal value = "+d);
		System.out.println("The Bitwise XOR(exclusive OR) : a ^ b = " + binary[e] + " and Decimal value = "+e);
		System.out.println("The Bitwise unary NOT : ~a & a = " + binary[f] + " and Decimal value = "+f);
		System.out.println("~a&b|a&~b = " + binary[g]+ " and Decimal value = "+g);
		
		System.out.println();
		System.out.println("<-------Bitewise Shift Operators------->");
		
		System.out.println("The original binary value of a = " +binary[a] + " and Decimal value of a = "+a);
		a = a << 2;		//Bitwise Left shift operator
		System.out.println("The Left shift : a = a << 2 means a = 0010 << 2 hence a = "+binary[a] + " and Decimal value of a = "+a);
		
		System.out.println("The original binary value of b = " +binary[b] + " and Decimal value of b = "+b);
		b = b >> 2;		//Bitwise Right shift operator
		System.out.println("The Right shift : b = b >> 2 means b =  0111 >> 2 hence b = "+binary[b] + " and Decimal value of b = "+b);
		
		int u = -1;
		System.out.println("The original decimal value of u = " +u);
		u = u >>> 30;	//Bitwise Unsigned Right shift operator	
		System.out.println("The Unsigned Right shift : u = u >>> 30 means u = 11111111 11111111 11111111 11111111 >>> 30 hence u = "+binary[u] + " and Decimal value of u = "+u);
		
		System.out.println();
		System.out.println("<-------Bitewise Assignment Operators------->");
		int p = 5;
		System.out.println("The original binary value of p = " +binary[p] + " and Decimal value of p = "+p);
		p >>= 2;		//Bitewise shift Right Assignment Operator
		System.out.println("The Bitewise Shift Right Assignment Operators : p >>= 2 means p = p >> 2 hence p = 0101 >> 2 so p = "+binary[p] + " and Decimal value of p = "+p);
		/*Same as you can check Bitwise AND assignment,Bitwise OR assignment,Bitwise exclusive OR assignment,
		  Shift right zero fill assignment,Shift left assignment */
	}
}

Output :
 
..............Bitewise operators.................
<-------Bitewise Logical Operators------->
The binary value of a = 0010
The binary value of b = 0111
The Bitwise OR : a | b = 0111 and Decimal value = 7
The Bitwise AND : a & b = 0010 and Decimal value = 2
The Bitwise XOR(exclusive OR) : a ^ b = 0101 and Decimal value = 5
The Bitwise unary NOT : ~a & a = 0000 and Decimal value = 0
~a&b|a&~b = 0101 and Decimal value = 5
 
<-------Bitewise Shift Operators------->
The original binary value of a = 0010 and Decimal value of a = 2
The Left shift : a = a << 2 means a = 0010 << 2 hence a = 1000 and Decimal value of a = 8
The original binary value of b = 0111 and Decimal value of b = 7
The Right shift : b = b >> 2 means b =  0111 >> 2 hence b = 0001 and Decimal value of b = 1
The original decimal value of u = -1
The Unsigned Right shift : u = u >>> 30 means u = 11111111 11111111 11111111 11111111 >>> 30 hence u = 0011 and Decimal value of u = 3
 
<-------Bitewise Assignment Operators------->
The original binary value of p = 0101 and Decimal value of p = 5
The Bitewise Shift Right Assignment Operators : p >>= 2 means p = p >> 2 hence p = 0101 >> 2 so p = 0001 and Decimal value of p = 1