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.
advertisement
import java.util.Scanner;
public class Bitwise_Operation
{
public static void main(String[] args)
{
int m, n, x, a;
Scanner s = new Scanner(System.in);
System.out.print("Enter First number:");
m = s.nextInt();
System.out.print("Enter Second number:");
n = s.nextInt();
while(true)
{
System.out.println("");
System.out.println("Press 1 for Right Shift by 2:");
System.out.println("Press 2 for Left Shift by 2:");
System.out.println("Press 3 for Bitwise AND:");
System.out.println("Press 4 for Bitwise OR by 2:");
System.out.println("Press 5 for Bitwise Exclusive OR:");
System.out.println("Press 6 for Bitwise NOT:");
System.out.println("Press 7 to Exit:");
System.out.println("");
System.out.print("Option:");
x = s.nextInt();
switch(x)
{
case 1:
a = m << 2;
System.out.println("Result after left shift by 2:"+a);
break;
case 2:
a = n >> 2;
System.out.println("Result after right shift by 2:"+a);
break;
case 3:
a = m & n;
System.out.println("Result after bitwise AND:"+a);
break;
case 4:
a = m | n;
System.out.println("Result after bitwise OR:"+a);
break;
case 5:
a = m ^ n;
System.out.println("Result after bitwise Exclusive OR:"+a);
break;
case 6:
a = ~ m;
System.out.println("Result after bitwise NOT:"+a);
break;
case 7:
System.exit(0);
}
}
}
}
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.
Next Steps:
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice Information Technology MCQs
- Buy Programming Books
- Buy Java Books
- Practice BCA MCQs
- Apply for Java Internship