1178 Java Utilities Package and Bit Manipulation Chapter (Professional web hosting)

1178 Java Utilities Package and Bit Manipulation Chapter 20 60 // buffer to build output 61 StringBuffer buffer = new StringBuffer( 35 ); 62 63 // for each bit append 0 or 1 to buffer 64 for ( int bit = 1; bit <= 32; bit++ ) { 65 66 // use displayMask to isolate bit and determine whether 67 // bit has value of 0 or 1 68 buffer.append( 69 ( value & displayMask ) == 0 ? '0' : '1' ); 70 71 // shift value one position to left 72 value <<= 1; 73 74 // append space to buffer every 8 bits 75 if ( bit % 8 == 0 ) 76 buffer.append( ' ' ); 77 } 78 79 return buffer.toString(); 80 } 81 82 // execute application 83 public static void main( String args[] ) 84 { 85 PrintBits application = new PrintBits(); 86 87 application.setDefaultCloseOperation( 88 JFrame.EXIT_ON_CLOSE ); 89 } 90 91 } // end class PrintBits Fig. 20.6Printing the bits in an integer (part 3 of 3). Fig. 20.6 Lines 68 69 append a 1 or a 0 to a StringBuffer for the current leftmost bit of variable value. Assume that value contains 4000000000 (11101110 01101011 00101000 00000000). When value and displayMask are combined using &, all the bits except the high-order (leftmost) bit in variable valueare masked off (hidden), because any bit ANDed with 0 yields 0. If the leftmost bit is 1, value &display- Maskevaluates to a nonzero value and 1is appended; otherwise, 0is appended. Then vari
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Leave a Reply