Source of BitShift.java


  1: // Fig. I.08: BitShift.java
  2: // Using the bitwise shift operators.
  3: import java.util.Scanner;
  4: 
  5: public class BitShift
  6: {
  7:    public static void main( String args[] )
  8:    {
  9:       int choice = 0; // store operation type
 10:       int input = 0; // store input integer
 11:       int result = 0; // store operation result
 12:       Scanner scanner = new Scanner( System.in ); // create Scanner
 13: 
 14:       // continue execution until user exit
 15:       while ( true )
 16:       {
 17:          // get shift operation
 18:          System.out.println( "\n\nPlease choose the shift operation:" );
 19:          System.out.println( "1--Left Shift (<<)" );
 20:          System.out.println( "2--Signed Right Shift (>>)" );
 21:          System.out.println( "3--Unsigned Right Shift (>>>)" );
 22:          System.out.println( "4--Exit" );
 23:          choice = scanner.nextInt();
 24: 
 25:          // perform shift operation
 26:          switch ( choice )
 27:          {
 28:             case 1: // <<
 29:                System.out.println( "Please enter an integer to shift:" );
 30:                input = scanner.nextInt(); // get input integer
 31:                result = input << 1; // left shift one position
 32:                System.out.printf( "\n%d << 1 = %d", input, result );
 33:                break;
 34:             case 2: // >>
 35:                System.out.println( "Please enter an integer to shift:" );
 36:                input = scanner.nextInt(); // get input integer
 37:                result = input >> 1; // signed right shift one position
 38:                System.out.printf( "\n%d >> 1 = %d", input, result );
 39:                break;
 40:             case 3: // >>>
 41:                System.out.println( "Please enter an integer to shift:" );
 42:                input = scanner.nextInt(); // get input integer
 43:                result = input >>> 1; // unsigned right shift one position
 44:                System.out.printf( "\n%d >>> 1 = %d", input, result );  
 45:                break;
 46:             case 4: default: // default operation is <<
 47:                System.exit( 0 ); // exit application
 48:          } // end switch
 49: 
 50:       // display input integer and result in bits
 51:       BitRepresentation.display( input );
 52:       BitRepresentation.display( result );
 53:       } // end while
 54:    } // end main
 55: } // end class BitShift
 56: 
 57: 
 58: /**************************************************************************
 59:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 60:  * Pearson Education, Inc. All Rights Reserved.                           *
 61:  *                                                                        *
 62:  * DISCLAIMER: The authors and publisher of this book have used their     *
 63:  * best efforts in preparing the book. These efforts include the          *
 64:  * development, research, and testing of the theories and programs        *
 65:  * to determine their effectiveness. The authors and publisher make       *
 66:  * no warranty of any kind, expressed or implied, with regard to these    *
 67:  * programs or to the documentation contained in these books. The authors *
 68:  * and publisher shall not be liable in any event for incidental or       *
 69:  * consequential damages in connection with, or arising out of, the       *
 70:  * furnishing, performance, or use of these programs.                     *
 71:  *************************************************************************/