Tutoriallearn.com

Easy Learning


Home

Search Tutoriallearn.com :




Java Operators


4. Bitwise Operators


Bitwise operators operates on individual bits of the operands. They can be applied to 'int', 'long', 'short', 'char' and 'byte' datatypes. Following table shows bitwise operators available in Java.
Operator Purpose
& Bitwise AND operation
| Bitwise OR operation
^ Bitwise Exclusive OR operation
~ Bitwise unary NOT
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
<<= Shift left assignment
>>>= Shift right zero fill assignment

Bitwise Operators Example

Following example is demonstrating Bitwise &, |, ^, and ~ operators:
import java.lang.Math;
public class BitOp {
public static void main (String args[]){

    int a = 9;
    int b = 10;
   
    int c = a & b;
    
    System.out.println("Bitwise & (AND) operation:");
	System.out.println(Integer.toBinaryString(a) + " & "+ Integer.toBinaryString(b)+" = "+ 
	              Integer.toBinaryString(c)+"\n");
	
	c = a | b;
	
	System.out.println("Bitwise | (OR) operation:");
	System.out.println(Integer.toBinaryString(a) + " | "+ Integer.toBinaryString(b)+" = "+  
	             Integer.toBinaryString(c)+"\n");
	
	c = a ^ b;
	
	System.out.println("Bitwise ^ (Exclusie-OR) operation:");
	System.out.println(Integer.toBinaryString(a) + " ^ "+ Integer.toBinaryString(b)+" =   "+ 
	            Integer.toBinaryString(c)+"\n");
	
	c = ~a;
	
    System.out.println("Bitwise Unary ~ (NOT) operation:");
	System.out.println(" ~"+Integer.toBinaryString(a)+ " = "+ Integer.toString(c, 2)+
	             "  (See the negative sign in the result)");
	
 }
}

Output of the program

Bitwise & (AND) operation:
1001 & 1010 = 1000

Bitwise | (OR) operation:
1001 | 1010 = 1011

Bitwise ^ (Exclusive-OR) operation:
1001 ^ 1010 =   11

Bitwise Unary ~ (NOT) operation:
 ~1001 = -1010  (See the negative sign in the result)
Following example is demonstrating Shift right (>>) and Shift Left (<<) operations.
It is also demonstrating Shift right zero fill (>>>) operator.
Shift right zero fill (>>>) operator is also called Unsigned Right Shift. This operator does not preserve sign of the value.
import java.lang.Math;
public class BitOp {
public static void main (String args[]){

    int a = 9;
    int b = 2;
	int m = -9;
   
    int c = a >> 1; // Shift right by 1 bit
    
    System.out.println("Bitwise >> (Shift Right) operation:");
	System.out.println(Integer.toBinaryString(a) + " >> 1 "+ " = "+Integer.toBinaryString(c)+"\n");
	
	c = b << 1; // Shift left by 1 bit
	
	System.out.println("Bitwise << (Shift Left) operation:");
	System.out.println(Integer.toBinaryString(b) + " << 1 " + " = "+Integer.toBinaryString(c)+"\n");
	
	c = m >>> 1; // Unsigned Right Shift by 1 bit
	
	System.out.println("Bitwise >>> (Unsigned Right Shift ) operation:");
	System.out.println(Integer.toBinaryString(m) + " >>> 1 " + " = "+String.format("%32s", Integer.toBinaryString(c)).replace(' ', '0')+"\n");
 }
}

Output of the program

Bitwise >> (Shift Right) operation:
1001 >> 1  = 100

Bitwise << (Shift Left) operation:
10 << 1  = 100

Bitwise >>> (Unsigned Right Shift ) operation:
11111111111111111111111111110111 >>> 1  = 01111111111111111111111111111011
Following example is demonstrating Bitwise Assignment Operators &=, |=, ^=
import java.lang.Math;
public class BitOpAssin {
public static void main (String args[]){

      int a = 9;
      int b = 10;
   
      System.out.println("a = "+Integer.toBinaryString(a));
      System.out.println("b = "+Integer.toBinaryString(b));
	
	System.out.println("Bitwise AND Assignment (&=)  operation:");
	System.out.println(Integer.toBinaryString(a) + " &=  "+ Integer.toBinaryString(b));
	
	a &= b;
	
	System.out.println("Now value of a = "+ Integer.toBinaryString(a)+"\n");
	
	int k = 9;
	
	System.out.println("k = "+Integer.toBinaryString(k));
	System.out.println("b = "+Integer.toBinaryString(b));
	
	System.out.println("Bitwise OR Assignment (|=)  operation:");
	System.out.println(Integer.toBinaryString(k) + " |=  "+ Integer.toBinaryString(b));
	
	k |= b;
	
	System.out.println("Now value of k = "+ Integer.toBinaryString(k)+"\n");
	
	int m = 9;
	
	System.out.println("m = "+Integer.toBinaryString(m));
	System.out.println("b = "+Integer.toBinaryString(b));
	
	System.out.println("Bitwise OR Assignment (^=)  operation:");
	System.out.println(Integer.toBinaryString(m) + " ^=  "+ Integer.toBinaryString(b));
	
	m ^= b;
	System.out.println("Now value of m = "+ Integer.toBinaryString(m)+"\n");	
}
}

Output of the Program

a = 1001
b = 1010
Bitwise AND Assignment (&=)  operation:
1001 &=  1010
Now value of a = 1000

k = 1001
b = 1010
Bitwise OR Assignment (|=)  operation:
1001 |=  1010
Now value of k = 1011

m = 1001
b = 1010
Bitwise OR Assignment (^=)  operation:
1001 ^=  1010
Now value of m = 11

Following example is demonstrating Right Shift Assignment operator (>>=) and Left Shift Assignment operator (<<=).
import java.lang.Math;
public class BitOpShiftAssin {
public static void main (String args[]){

    int a = 9;
    int b = 2;
   
    System.out.println("a = "+Integer.toBinaryString(a));
    System.out.println("Bitwise Right Shift Assignment (>>=) operation:");
	System.out.println(Integer.toBinaryString(a) + " >>= 1 ");
	
    a >>= 1; // Shift right by 1 bit
    
    System.out.println("Now value of a ="+Integer.toBinaryString(a));
	
	System.out.println();
	System.out.println("b = "+Integer.toBinaryString(b));
	System.out.println("Bitwise Left Shift Assignment (<<=) operation:");
	System.out.println(Integer.toBinaryString(b) + " <<= 1 ");
	
	b <<= 1; // Shift left by 1 bit
	
	System.out.println("Now value of b ="+Integer.toBinaryString(b));
 }
}

Output of the program

a = 1001
Bitwise Right Shift Assignment (>>=) operation:
1001 >>= 1 
Now value of a =100

b = 10
Bitwise Left Shift Assignment (<<=) operation:
10 <<= 1 
Now value of b =100


© Copyright 2016-2025 by tutoriallearn.com. All Rights Reserved.