1182 Java Utilities Package and Bit Manipulation Chapter (Space web hosting)
Friday, July 27th, 20071182 Java Utilities Package and Bit Manipulation Chapter 20 118 119 // button to perform bitwise complement 120 JButton complementButton = new JButton( “Complement” ); 121 122 complementButton.addActionListener( 123 124 new ActionListener() { 125 126 // perform bitwise complement and display results 127 public void actionPerformed( ActionEvent event ) 128 { 129 input2Field.setText( “” ); 130 bits2Field.setText( “” ); 131 132 int value = Integer.parseInt( input1Field.getText() ); 133 134 resultField.setText( Integer.toString( ~value ) ); 135 bits1Field.setText( getBits( value ) ); 136 bits3Field.setText( getBits( ~value ) ); 137 } 138 } 139 ); 140 141 buttonPanel.add( complementButton ); 142 143 Container container = getContentPane(); 144 container.add( inputPanel, BorderLayout.WEST ); 145 container.add( bitsPanel, BorderLayout.EAST ); 146 container.add( buttonPanel, BorderLayout.SOUTH ); 147 148 setSize( 600, 150 ); 149 setVisible( true ); 150 } 151 152 // display numbers and their bit form 153 private void setFields() 154 { 155 value1 = Integer.parseInt( input1Field.getText() ); 156 value2 = Integer.parseInt( input2Field.getText() ); 157 158 bits1Field.setText( getBits( value1 ) ); 159 bits2Field.setText( getBits( value2 ) ); 160 } 16 16 // display bit representation of specified int value 16 private String getBits( int value ) 16 { 16 // create int value with 1 in leftmost bit and 0s elsewhere 16 int displayMask = 1 << 31; 16 16 // buffer to build output 16 StringBuffer buffer = new StringBuffer( 35 ); Fig. 20.8Demonstrating the bitwise AND, bitwise inclusive OR, bitwise exclusive OR Fig. 20.8 and bitwise complement operators (part 4 of 6).
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.