// Fig. 18.08: MiscBitOps.java // Using the bitwise AND, bitwise inclusive OR, bitwise // exclusive OR, and bitwise complement operators. import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class MiscBitOps extends Applet implements ActionListener { private TextField input1, input2, result, bits1, bits2, bits3; private Button and, inclusiveOr, exclusiveOr, complement; private Panel inputPanel, bitsPanel, buttonPanel; public void init() { setLayout( new BorderLayout() ); inputPanel = new Panel(); inputPanel.setLayout( new GridLayout( 4, 2 ) ); inputPanel.add( new Label( "Enter 2 ints" ) ); inputPanel.add( new Label( "" ) ); inputPanel.add( new Label( "Value 1" ) ); input1 = new TextField( 8 ); inputPanel.add( input1 ); inputPanel.add( new Label( "Value 2" ) ); input2 = new TextField( 8 ); inputPanel.add( input2 ); inputPanel.add( new Label( "Result" ) ); result = new TextField( 8 ); result.setEditable( false ); inputPanel.add( result ); bitsPanel = new Panel(); bitsPanel.setLayout( new GridLayout( 4, 1 ) ); bitsPanel.add( new Label( "Bit representations" ) ); bits1 = new TextField( 33 ); bits1.setEditable( false ); bitsPanel.add( bits1 ); bits2 = new TextField( 33 ); bits2.setEditable( false ); bitsPanel.add( bits2 ); bits3 = new TextField( 33 ); bits3.setEditable( false ); bitsPanel.add( bits3 ); buttonPanel = new Panel(); and = new Button( "AND" ); and.addActionListener( this ); buttonPanel.add( and ); inclusiveOr = new Button( "Inclusive OR" ); inclusiveOr.addActionListener( this ); buttonPanel.add( inclusiveOr ); exclusiveOr = new Button( "Exclusive OR" ); exclusiveOr.addActionListener( this ); buttonPanel.add( exclusiveOr ); complement = new Button( "Complement" ); complement.addActionListener( this ); buttonPanel.add( complement ); add( inputPanel, BorderLayout.WEST ); add( bitsPanel, BorderLayout.EAST ); add( buttonPanel, BorderLayout.SOUTH ); } public void actionPerformed( ActionEvent e ) { if ( e.getSource() == complement ) { input2.setText( "" ); bits2.setText( "" ); int val = Integer.parseInt( input1.getText() ); result.setText( Integer.toString( ~val ) ); bits1.setText( getBits( val ) ); bits3.setText( getBits( ~val ) ); } else { int val1 = Integer.parseInt( input1.getText() ); int val2 = Integer.parseInt( input2.getText() ); bits1.setText( getBits( val1 ) ); bits2.setText( getBits( val2 ) ); if ( e.getSource() == and ) { result.setText( Integer.toString( val1 & val2 ) ); bits3.setText( getBits( val1 & val2 ) ); } else if ( e.getSource() == inclusiveOr ) { result.setText( Integer.toString( val1 | val2 ) ); bits3.setText( getBits( val1 | val2 ) ); } else if ( e.getSource() == exclusiveOr ) { result.setText( Integer.toString( val1 ^ val2 ) ); bits3.setText( getBits( val1 ^ val2 ) ); } } } public String getBits( int value ) { int displayMask = 1 << 31; StringBuffer buf = new StringBuffer( 35 ); for ( int c = 1; c <= 32; c++ ) { buf.append( ( value & displayMask ) == 0 ? '0' : '1' ); value <<= 1; if ( c % 8 == 0 ) buf.append( ' ' ); } return buf.toString(); } }