public class BitRepresentation
1: // Fig I.5: BitRepresentation.java
2: // Utility class that display bit representation of an integer.
3:
4: public class BitRepresentation
5: {
6: // display bit representation of specified int value
7: public static void display( int value )
8: {
9: System.out.printf( "\nBit representation of %d is: \n", value );
10:
11: // create int value with 1 in leftmost bit and 0s elsewhere
12: int displayMask = 1 << 31;
13:
14: // for each bit display 0 or 1
15: for ( int bit = 1; bit <= 32; bit++ )
16: {
17: // use displayMask to isolate bit
18: System.out.print( ( value & displayMask ) == 0 ? '0' : '1' );
19:
20: value <<= 1; // shift value one position to left
21:
22: if ( bit % 8 == 0 )
23: System.out.print( ' ' ); // display space every 8 bits
24: } // end for
25: } // end method display
26: } // end class BitRepresentation
27:
28: /**************************************************************************
29: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
30: * Pearson Education, Inc. All Rights Reserved. *
31: * *
32: * DISCLAIMER: The authors and publisher of this book have used their *
33: * best efforts in preparing the book. These efforts include the *
34: * development, research, and testing of the theories and programs *
35: * to determine their effectiveness. The authors and publisher make *
36: * no warranty of any kind, expressed or implied, with regard to these *
37: * programs or to the documentation contained in these books. The authors *
38: * and publisher shall not be liable in any event for incidental or *
39: * consequential damages in connection with, or arising out of, the *
40: * furnishing, performance, or use of these programs. *
41: *************************************************************************/
42: