Java

How to Use Bitwise Operators in Java

In Java, a bitwise operator is an operator that manipulates individual bits to execute bitwise operations. Each bit has a binary value of either 0 or 1. There are multiple bitwise operators in Java such as AND, OR, Exclusive OR, Inclusive OR, Compliment, and bit shift operators.

The majority of programming languages work with 8-bit, 16-bit, or 32-bit groups. It is also known as bit-level programming that is mostly utilized in numerical computations to speed up the process. So in this article, we will guide you on how you can calculate the bitwise operator using the Java programming language.

How to calculate the bitwise AND (&) in Java

Java provides a very simple and straightforward method for users to enter values via the keyboard. For this, a java.util.Scanner class is utilized:

import java.util.Scanner;

We have created a public class in which we will write the main code:

Public class BitwiseOperators

After defining the main function,  we have created a scanner object which is used to scan the keys that will be pressed from the keyboard by the user:

Scanner in=new Scanner(System.in);

Now, we have initialized two integers a, b and then we have taken input from the user using the in.nextInt() function:

int a, b;
        System.out.println("Please enter a number ");
  a=in.nextInt();
        System.out.println("Please enter another number ");
        b=in.nextInt();

Now we are going to implement the Bitwise (AND) operator as shown below:

int c = a & b;
System.out.println(a+" & " + b + " = " + c);

Bitwise operator works using binary values of a decimal number so, suppose we have taken a value of 12 for variable a and 5 for variable b. The binary value of 12 is 1100 whereas the binary value of 5 is 0101 so if we apply the bitwise (AND) operator between them then we get the value of 4 as shown below.

Decimal   Binary
12 = 1 1 0 0
    & & & &
5 = 0 1 0 1
4 = 0 1 0 0

So the result shows that in the AND (&) operator if the value for both A and B is 1 then the output value will also be 1 otherwise it will be zero for all other cases.

How to calculate the bitwise OR (|) in Java

Now we are going to calculate another operator which is bitwise OR (|)

int a, b;
System.out.println("Please enter a number ");
a=in.nextInt();
System.out.println("Please enter another number ");
b=in.nextInt();

Now we are going to implement the Bitwise (OR) operator as shown below:

int c = a | b;
System.out.println(a+" | " + b + " = " + c);

Now we will discuss an example for OR bitwise operator by taking the same values as above

Decimal   Binary
12 = 1 1 0 0
    | | | |
5 = 0 1 0 1
13 = 1 1 0 1

[/cc]

So, in this case, the result shows that if the value of A or B is one then the output will also be one and if both the values are zero then the output will also be zero. Now to complete the code to calculate the bitwise OR operator is mentioned below:

import java.util.Scanner;
public class BitwiseOperators
{
    public static void main(String[] args) {
        //Declare a Scanner object
    Scanner in=new Scanner(System.in);
    //Bitwise operators Example 1:
        System.out.println("Please enter a number ");
        int a, b;
        a=in.nextInt();
        System.out.println("Please enter another number ");
        b=in.nextInt();
        //Performing bitwise AND (&)
        int c = a & b;
        System.out.println(a+" & " + b + " = " + c);
        c = a | b;
        System.out.println(a+" | " + b + " = " + c);
    }
}

Note: Before creating a java file you need to make sure that the java development kit (JDK) is installed in your Linux operating system.

Now you need to open any text editor that is available in your Linux operating system and then write the above-mentioned code inside it as shown below:

$ nano BitwiseOperators

$ javac BitwiseOperators.java
$ java BitwiseOperators

How to use bit shift operators in Java

Now we have created another function where we are going to discuss the right (>>) and left (<<) bit shifting operator:

import java.util.Scanner;
public class BitwiseOperatorsb
{
    public static void main(String[] args) {
        //Declare a Scanner object
        Scanner in=new Scanner(System.in);
        System.out.println("Please enter a number ");
        int x;
        x=in.nextInt();
        System.out.println("Please enter number of bits ");
        y=in.nextInt();
        //Performing right bit-shifting (>>)
        int Output1 = x>>y;
        System.out.println(" Result after right bit-shifting is " + Output1);
        //Performing left bit-shifting (<<)
        int Output2 = x<<y;
        System.out.println(" Result after right bit-shifting is " + Output2);
    }
}

A complete list of commonly used bitwise operators is mentioned below:

Operator Symbol
AND &
Inclusive OR |
Exclusive OR ^
Compliment ~
Left Shift <<
Right Shift >>

 Conclusion

A bitwise operator manipulates individual bits to perform bitwise operations, with each bit having a binary value of 0 or 1. It’s also known as bit-level programming, and it’s commonly used to speed up numerical operations. This article has taught you how to implement the bitwise operator using the Java programming language.

About the author

Taimoor Mohsin