Bitwise Operator In JAVA With Example

As from the name Bitwise sound these operator performs the operation on bit value. And also you must have heard bit is smallest unit of memory. This is represented by either 0 or 1 which means you have only one option to mark your answer. Also you can understand this concept by an electrical switch which is connected with a bulb. There can only be two conditions, one is bulb can be ON and second is it can be OFF. This is exact representation of a single bit. The combination of 4 bits is called as nibble. Bitwise common Operators are: Bitwise AND (&), Bitwise OR(|) and Bitwise exclusive OR(^):


Binary Number System:

Before you understand Bitwise operator it is very important to understand Binary Number System. As binary number indicates that there can be only two values can be represented here either 0 or 1 which makes it a complete binary system. Below table describe the addition and multiplication of two Bit Value.

A B Addition Multiplication
0 0 0 0
0 1 1 0
1 0 1 0
1 1 10 1

Important Note: There is also a difference between AND (&) and logical AND (&&). Because logical AND (&&) can only result in the Boolean value i.e. it can be either 0 or 1. And in other words we can say it can be true or false, it can be recognized as on or off.


Bitwise Different Operator:

Now we discuss about different Bitwise operator: Bitwise AND (&), Bitwise OR(|) and Bitwise exclusive OR(^).

Bitwise And (&):

In case of AND(&), result depend on the value of bit value of comparative operands. In other words, only if both operands Bit value is 1 then only the result is 1. Below is the Bitwise And (&) table:

A B AND(&)
0 0 0
0 1 0
1 0 0
1 1 1

Example, consider in the given program there are two variables considered a and b having two different values.

a=3; b=4;

if we calculate respective binary value of both the numbers we get:

a=011;

b=100;

As AND work on bit values and if we calculate the result for this (please refer & table):

a&b=000;

Now if we calculate the number value of result bit values(000) is equal to 0. Because in this case there bit value respective to other operand bit values are different and hence the result generated is 000. And the same result (0) is produced by the program.

Bitwise Inclusive OR (|)

Now for consideration of Bitwise OR (|) firstly understand the basic behind it. This also works on bit value. In this the logic result 1 if any one of bit value is 1. The result will be zero only if both the values are zero.

It can be more cleared from the table shown below:

A B Inclusive OR (|)
0 0 0
0 1 1
1 0 1
1 1 1

From the code view of point there are two variable considered
A=3; // 011
B=4 //100
A|B=7 //111

Hence the result obtained from these variables is given as 7. Because here we apply inclusive OR (|) operator in it.

Bitwise Exclusive OR (^)

So in case of exclusive OR (^) the result value is only one when both the bit values are different means when one is 0 and other value is 1 and also if first one is 1 and second one 0, in both the cases the result value will be 1. And if both are having same value like 0 and 1 then result will be zero (0).

A B Exclusive OR
0 0 0
0 1 1
1 0 1
1 1 0

As from program there are two variables

a=3;       //011

b=4;       //100

a^b=7;  //111

Here a is having bit values as 011 and b having 100 as bit values in binary number system. And in the resultant if take it its exclusive OR(^) all bit values are different in respect to each other so the resultant comes as 111 which is count to be as 7 in decimal number system. And hence the output from the respective code of program.


Bitwise Operator Example in JAVA

Now lets create a program in JAVA using all three Bitwise AND (&), Bitwise OR(|), and Bitwise exclusive OR(^) operator.

public class BitwiseExample {

    public static void main(String[] args) {
        int a=3; //binary value is 011
        int b=4; //binary value is 100
        int e=a&b; // result value is 000 which is 0
        int f=a|b;// result value is 111 which is 7
        int g=a^b;// result value is 111 which is 7
        System.out.println("The value of a & b(AND) is: " + e);
        System.out.println("The value of a | b(INCLUSIVE OR) is: " + f);
        System.out.println("The value of a ^ b(EXCLUSIVE OR) is: "+ g);
        
    }

}

Output:

The value of a & b(AND) is: 0
The value of a | b(INCLUSIVE OR) is: 7
The value of a ^ b(EXCLUSIVE OR) is: 7