Signup/Sign In
PUBLISHED ON: APRIL 23, 2021

Java Program to Perform Bitwise Operations

In this tutorial, we will learn how to perform bitwise operations by taking input from the user. Bitwise operators are the operators that are used to manipulate individual bits of a number. These are highly used while performing update and query operations on a binary indexed tree. It is applied to the integer types, long, int, short, char, and byte. But before moving further, if you are not familiar with the concept of the bitwise operator in java, then do check the article on Operators in Java.

Input: ( 5 & 4 )

( 5 | 4 )

( 5 ^ 4 )

( 5 << 4 )

( 5 >> 4 )

Output:

4

5

1

80

0

Two cases arise for the above problem:

Case 1: When values are user-defined

Case 2: When values are pre-defined

Let us look at each of these cases separately.

Program 1: To Perform Bitwise Operation

In this program, we will see how to perform bitwise operations in java when the values are user-defined. Here, firstly we will ask the user to input the values, and then we will perform the bitwise operations.

Algorithm:

  1. Start
  2. Here, we will use a switch case to choose from different bitwise operators like &, |, ^, ~, <<, and >>.
  3. Declare a variable for the same.
  4. Ask the user to initialize it.
  5. Based on the operation chosen, declare two variables.
  6. Ask the user to initialize the variables.
  7. Display the result after performing the bitwise operations.
  8. Stop.

Let us look at the below example for a better understanding.

//Java Program to perform the bitwise operation
import java.util.Scanner;
public class Main
{
   public static void main(String args[])
   {   
       //Take input from the user
       //Create instance of the Scanner class
        Scanner s = new Scanner(System.in);
        while(true)
        {
            System.out.println("");
            
            System.out.println("Choose the operation you want to perform ");
            System.out.println("Choose 1 for & ");
            System.out.println("Choose 2 for | ");
            System.out.println("Choose 3 for ^ ");
            System.out.println("Choose 4 for ~");
            System.out.println("Choose 5 for <<");
            System.out.println("Choose 6 for >>");
            System.out.println("Choose 7 for EXIT");
            int n = s.nextInt();
            switch(n)
            {
                case 1:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int x = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int y = s.nextInt();
                    System.out.println("Result of "+x+"&"+y+" = " + (x&y));
                    break;
 
                case 2:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int p = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int q = s.nextInt();
                    System.out.println("Result of "+p+"|"+q+" = " + (p |q ));
                    break;
 
                case 3:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int a = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int b = s.nextInt();
                    System.out.println("Result of "+a+"^"+b+" = " + (a ^ b));
                    break;
 
                case 4:
                    System.out.print("Enter the number : ");
                    int c = s.nextInt();
                    System.out.print("The result of ~"+c+ " is "+(~c));
                    break;
 
                case 5:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int e = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int f = s.nextInt();
                    System.out.println("Result of "+e+"<<"+f+" = " + (e<<f));
                    break;
                    
                case 6:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int g = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int h = s.nextInt();
                    System.out.println("Result of "+g+">>"+h+" = " + (g>>h));
                    break;
                case 7:
                    System.exit(0);
            }
        }
    }
}


Choose the operation you want to perform
Choose 1 for &
Choose 2 for |
Choose 3 for ^
Choose 4 for ~
Choose 5 for <<
Choose 6 for >>
Choose 7 for EXIT
1
Enter the two numbers to perform operations
Enter the first number: 2
Enter the second number: 3
Result of 2&3 = 2

Choose the operation you want to perform
Choose 1 for &
Choose 2 for |
Choose 3 for ^
Choose 4 for ~
Choose 5 for <<
Choose 6 for >>
Choose 7 for EXIT
2
Enter the two numbers to perform operations
Enter the first number: 4
Enter the second number: 5
Result of 4|5 = 5

Choose the operation you want to perform
Choose 1 for &
Choose 2 for |
Choose 3 for ^
Choose 4 for ~
Choose 5 for <<
Choose 6 for >>
Choose 7 for EXIT
3
Enter the two numbers to perform operations
Enter the first number: 5
Enter the second number:6
Result of 5^6 = 3

Choose the operation you want to perform
Choose 1 for &
Choose 2 for |
Choose 3 for ^
Choose 4 for ~
Choose 5 for <<
Choose 6 for >>
Choose 7 for EXIT
4
Enter the number: The result of ~7 is -8
Choose the operation you want to perform
Choose 1 for &
Choose 2 for |
Choose 3 for ^
Choose 4 for ~
Choose 5 for <<
Choose 6 for >>
Choose 7 for EXIT
5
Enter the two numbers to perform operations
Enter the first number: 8
Enter the second number: 9
Result of 8<<9 = 4096

Choose the operation you want to perform
Choose 1 for &
Choose 2 for |
Choose 3 for ^
Choose 4 for ~
Choose 5 for <<
Choose 6 for >>
Choose 7 for EXIT
6
Enter the two numbers to perform operations
Enter the first number: 1
Enter the second number: 2
Result of 1>>2 = 0

Choose the operation you want to perform
Choose 1 for &
Choose 2 for |
Choose 3 for ^
Choose 4 for ~
Choose 5 for <<
Choose 6 for >>
Choose 7 for EXIT
7

Program 2: To Perform Bitwise Operation

In this program, we will perform the bitwise operations when the values are pre-defined in the program.

Algorithm:

  1. Start
  2. Here, we will use a switch case to choose from different bitwise operators like &, |, ^, ~, <<, and >>.
  3. Declare two variables.
  4. Initialize it.
  5. Perform all the bitwise operators like &, |, ^, ~, <<, and >>.
  6. Display the result of each bitwise operation.
  7. Stop.

Let us look at the below example for a better understanding.

//Java Program to perform bitwise operation
public class Main 
{
    public static void main(String[] args)
    {
        // Declare and initialize the variables
        int a = 3;
        int b = 2;
        // bitwise and
        System.out.println("Result of "+a+"&"+b+" = " + (a & b));
        // bitwise or
        System.out.println("Result of "+a+"|"+b+" = " + (a | b));
        // bitwise xor
        System.out.println("Result of "+a+"^"+b+" = " + (a ^ b));
        System.out.println("Result of ~"+a+" = " + ~a);
        System.out.println("Result of "+a+" << "+b+" = " + (a << b));
        System.out.println("Result of "+a+" >> "+b+" = " + (a >> b));
        // When combined with assignment operator 
        a &= b;
        System.out.println("Result after a&=b is a= " + a);
    }
}


Result of 3&2 = 2
Result of 3|2 = 3
Result of 3^2 = 1
Result of ~3 = -4
Result of 3 << 2 = 12
Result of 3 >> 2 = 0
Result after a&=b is a= 2



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.