Bitwise Operators in Java

«
»
This is a Java Program to Illustrate the Use of Various Bitwise Operators.

Enter any two decimal numbers as an input. After that select any option from different available options to perform various biwise operations and get the desired output.

Here is the source code of the Java Program to Illustrate the Use of Various Bitwise Operators. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Bitwise_Operation 
  3. {
  4.    public static void main(String[] args) 
  5.     {
  6.         int m, n, x, a;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter First number:");
  9.         m = s.nextInt();
  10.         System.out.print("Enter Second number:");
  11.         n = s.nextInt();
  12.         while(true)
  13.         {
  14.             System.out.println("");
  15.             System.out.println("Press 1 for Right Shift by 2:");
  16.             System.out.println("Press 2 for Left Shift by 2:");
  17.             System.out.println("Press 3 for Bitwise AND:");
  18.             System.out.println("Press 4 for Bitwise OR by 2:");
  19.             System.out.println("Press 5 for Bitwise Exclusive OR:");
  20.             System.out.println("Press 6 for Bitwise NOT:");
  21.             System.out.println("Press 7 to Exit:");
  22.             System.out.println("");
  23.             System.out.print("Option:");
  24.             x = s.nextInt();
  25.             switch(x)
  26.             {
  27.                 case 1:
  28.                 a = m << 2;
  29.                 System.out.println("Result after left shift by 2:"+a);
  30.                 break;
  31.  
  32.                 case 2:
  33.                 a = n >> 2;
  34.                 System.out.println("Result after right shift by 2:"+a);   
  35.                 break;
  36.  
  37.                 case 3:
  38.                 a = m & n;
  39.                 System.out.println("Result after bitwise AND:"+a);  
  40.                 break;
  41.  
  42.                 case 4:  
  43.                 a = m | n;
  44.                 System.out.println("Result after bitwise OR:"+a);
  45.                 break;
  46.  
  47.                 case 5:
  48.                 a = m ^ n;
  49.                 System.out.println("Result after bitwise Exclusive OR:"+a);
  50.                 break;    
  51.  
  52.                 case 6:
  53.                 a = ~ m;
  54.                 System.out.println("Result after bitwise NOT:"+a);
  55.                 break;    
  56.  
  57.                 case 7:
  58.                 System.exit(0);
  59.             }
  60.         }
  61.      }
  62. }

Output:

$ javac Bitwise_Operation.java
$ java Bitwise_Operation
 
Enter First number:5
Enter Second number:6
 
Press 1 for Right Shift by 2:
Press 2 for Left Shift by 2:
Press 3 for Bitwise AND:
Press 4 for Bitwise OR by 2:
Press 5 for Bitwise Exclusive OR:
Press 6 for Bitwise NOT:
Press 7 to Exit:
 
Option:3
Result after bitwise AND:4
 
Press 1 for Right Shift by 2:
Press 2 for Left Shift by 2:
Press 3 for Bitwise AND:
Press 4 for Bitwise OR by 2:
Press 5 for Bitwise Exclusive OR:
Press 6 for Bitwise NOT:
Press 7 to Exit:
 
Option:7

Sanfoundry Global Education & Learning Series –- 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.