public class MiscBitOps
1: // Fig. I.4: MiscBitOps.java
2: // Using the bitwise operators.
3: import java.util.Scanner;
4:
5: public class MiscBitOps
6: {
7: public static void main( String args[] )
8: {
9: int choice = 0; // store operation type
10: int first = 0; // store first input integer
11: int second = 0; // store second input integer
12: int result = 0; // store operation result
13: Scanner scanner = new Scanner( System.in ); // create Scanner
14:
15: // continue execution until user exit
16: while( true )
17: {
18: // get selected operation
19: System.out.println( "\n\nPlease choose the operation:" );
20: System.out.printf( "%s%s", "1--AND\n2--Inclusive OR\n",
21: "3--Exclusive OR\n4--Complement\n5--Exit\n" );
22: choice = scanner.nextInt();
23:
24: // perform bitwise operation
25: switch ( choice )
26: {
27: case 1: // AND
28: System.out.print( "Please enter two integers:" );
29: first = scanner.nextInt(); // get first input integer
30: BitRepresentation.display( first );
31: second = scanner.nextInt(); // get second input integer
32: BitRepresentation.display( second );
33: result = first & second; // perform bitwise AND
34: System.out.printf(
35: "\n\n%d & %d = %d", first, second, result );
36: BitRepresentation.display( result );
37: break;
38: case 2: // Inclusive OR
39: System.out.print( "Please enter two integers:" );
40: first = scanner.nextInt(); // get first input integer
41: BitRepresentation.display( first );
42: second = scanner.nextInt(); // get second input integer
43: BitRepresentation.display( second );
44: result = first | second; // perform bitwise inclusive OR
45: System.out.printf(
46: "\n\n%d | %d = %d", first, second, result );
47: BitRepresentation.display( result );
48: break;
49: case 3: // Exclusive OR
50: System.out.print( "Please enter two integers:" );
51: first = scanner.nextInt(); // get first input integer
52: BitRepresentation.display( first );
53: second = scanner.nextInt(); // get second input integer
54: BitRepresentation.display( second );
55: result = first ^ second; // perform bitwise exclusive OR
56: System.out.printf(
57: "\n\n%d ^ %d = %d", first, second, result );
58: BitRepresentation.display( result );
59: break;
60: case 4: // Complement
61: System.out.print( "Please enter one integer:" );
62: first = scanner.nextInt(); // get input integer
63: BitRepresentation.display( first );
64: result = ~first; // perform bitwise complement on first
65: System.out.printf( "\n\n~%d = %d", first, result );
66: BitRepresentation.display( result );
67: break;
68: case 5: default:
69: System.exit( 0 ); // exit application
70: } // end switch
71: } // end while
72: } // end main
73: } // end class MiscBitOps
74:
75: /**************************************************************************
76: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
77: * Pearson Education, Inc. All Rights Reserved. *
78: * *
79: * DISCLAIMER: The authors and publisher of this book have used their *
80: * best efforts in preparing the book. These efforts include the *
81: * development, research, and testing of the theories and programs *
82: * to determine their effectiveness. The authors and publisher make *
83: * no warranty of any kind, expressed or implied, with regard to these *
84: * programs or to the documentation contained in these books. The authors *
85: * and publisher shall not be liable in any event for incidental or *
86: * consequential damages in connection with, or arising out of, the *
87: * furnishing, performance, or use of these programs. *
88: *************************************************************************/