Java Bitwise Operators: Examples and Usage
Table of Content:
The following program is a simple example which demonstrates the Bitwise operators. Copy and paste the following Java program in BitwiseAndOperator.java file, and compile and run this program
Bitwise AND Operator
class BitwiseAndOperator {
public static void main(String[] args){
int A = 10;
int B = 3;
int Y;
Y = A & B;
System.out.println(Y);
}
}
Output
2 Press any key to continue . . .
Bitwise OR Operator
class BitwiseOrOperator {
public static void main(String[] args){
int A = 10;
int B = 3;
int Y;
Y = A | B;
System.out.println(Y);
}
}
Output
11 Press any key to continue . . .
Bitwise XOR Operator
class BitwiseXOROperator {
public static void main(String[] args){
int A = 10;
int B = 3;
int Y;
Y = A ^ B;
System.out.println(Y);
}
}
Output
9 Press any key to continue . . .
Bitwise Compliment Operator
class BitwiseComplementOperator {
public static void main(String[] args){
int A = 10;
int Y;
Y = ~A ;
System.out.println(Y);
}
}
Output
-11 Press any key to continue . . .
Binary Left Shift Operator
class LeftShift {
public static void main(String args[]){
int x= 5;
int y;
y = x << 2 ;
System.out.println(y);
}
}
Output
20 Press any key to continue . . .
Binary Right Shift Operator
class RightShift {
public static void main(String args[]){
int x= 5;
int y;
y = x >> 2 ;
System.out.println(y);
}
}
Output
1 Press any key to continue . . .
Zero fill right shift
class ZeroFillRightShift {
public static void main(String args[]){
int x= 10;
int y;
y = x >>> 2 ;
System.out.println(y);
}
}
Output
2 Press any key to continue . . .
Bitwise Operator all in one
public class BitwiseOperator {
public static void main(String args[]) {
int p = 60; /* 60 = 0011 1100 */
int q = 13; /* 13 = 0000 1101 */
int c = 0;
c = p & q; /* 12 = 0000 1100 */
System.out.println("p & q = " + c );
c = p | q; /* 61 = 0011 1101 */
System.out.println("p | q = " + c );
c = p ^ q; /* 49 = 0011 0001 */
System.out.println("p ^ q = " + c );
c = ~p; /*-61 = 1100 0011 */
System.out.println("~p = " + c );
c = p << 2; /* 240 = 1111 0000 */
System.out.println("p << 2 = " + c );
c = p >> 2; /* 15 = 1111 */
System.out.println("p >> 2 = " + c );
c = p >>> 2; /* 15 = 0000 1111 */
System.out.println("p >>> 2 = " + c );
}
}
Output
p & q = 12 p | q = 61 p ^ q = 49 ~p = -61 p << 2 = 240 p >> 2 = 15 p >>> 2 = 15 Press any key to continue . . .
Bitwise complement operator
If a = 10 then What will be the output of ~ a ?
a = 10 ( here no sign)
a = +10 (no sing means it is positive)
a = 0 1010 (in binary Sign and Magnitude)
a = 0 1010 (in two’s complement form)
a = 0 1010 (in two’s complement form) b = ~a = 1 0101 ( Here we performed Bitwise Complement operation) Now b = - 5 ( But this is not our Result)
So What Will be the Solution
We have to convert the b = 1 0101 in two’s complement form because here the sign bit is 1 that means it is negative.
b = 1 0101
Now two’s complement of b means (1’s complement 0f 0101 + 1), here no need to consider sign bit.
- 0101 one’s complement result will be 1010
- 1010 +1 = 1011 (two’s complement of b)
- So our final result will be b = 1 1011
- b = 1 1011
- b = - 11
a = -10 then What will be the output of ~ a ?
a = -10 ( Here sign exist ) a = 1 1010 (in binary Sign and Magnitude) a = 1 0110 (in two’s complement form)
a = 1 0110 (in two’s complement form) b = ~ a = 0 1001 ( Here we performed Bitwise Complement operation) Now, b = +9 ( Our Result )
Overview:
- Assignment 1: Shift Operator Example: Left Shift in Java
- Assignment 2: Shift Operator Example: Right Shift in Java
- Assignment 3: Shift Operator Example: >> vs >>> in Java