Chapter 20 Java Utilities (Cpanel web hosting) Package and Bit Manipulation
Chapter 20 Java Utilities Package and Bit Manipulation 1187 80 bitsField.setText( getBits( value ) ); 81 } 82 } 83 ); 84 85 container.add( rightSignButton ); 86 87 // button to right shift value one position with zero extension 88 JButton rightZeroButton = new JButton( “>>>” ); 89 90 rightZeroButton.addActionListener( 91 92 new ActionListener() { 93 94 // right shift one position and display new value 95 public void actionPerformed( ActionEvent event ) 96 { 97 int value = Integer.parseInt( valueField.getText() ); 98 value >>>= 1; 99 valueField.setText( Integer.toString( value ) ); 100 101 bitsField.setText( getBits( value ) ); 102 } 103 } 104 ); 105 106 container.add( rightZeroButton ); 107 108 setSize( 400, 120 ); 109 setVisible( true ); 110 } 111 112 // display bit representation of specified int value 113 private String getBits( int value ) 114 { 115 // create int value with 1 in leftmost bit and 0s elsewhere 116 int displayMask = 1 << 31; 117 118 // buffer to build output 119 StringBuffer buffer = new StringBuffer( 35 ); 120 121 // for each bit append 0 or 1 to buffer 122 for ( int bit = 1; bit <= 32; bit++ ) { 123 124 // use displayMask to isolate bit and determine whether 125 // bit has valueof 0 or1 126 buffer.append( 127 ( value & displayMask ) == 0 ? '0' : '1' ); 128 129 // shift value one position to left 130 value <<= 1; 131 Fig. 20.11 Fig. 20.11 Demonstrating the bitwise shift operators (part 3 of 5).
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.