
import java.applet.*;
import java.awt.*;

public class IEEE754
  extends Applet
{
  private Panel pUI;
  private Panel pNum;
  private Label lNum;
  private TextField tfNum;
  private Panel pConv;
  private Label lConv;
  private Button bConv;
  private Panel pRepD;
  private TextArea tfRepD;

  public IEEE754()
  {
    pNum = new Panel();
    pNum.setLayout( new FlowLayout( FlowLayout.LEFT ) );
    lNum = new Label( "Number:" );
    tfNum = new TextField(40);
    pNum.add( lNum );
    pNum.add( tfNum );

    pConv = new Panel();
    pConv.setLayout( new FlowLayout( FlowLayout.LEFT ) );
    lConv = new Label( "Press button to get IEEE754 Representation" );
    bConv = new Button( "=>" );
    pConv.add( lConv );
    pConv.add( bConv );

    pRepD = new Panel();
    pRepD.setLayout( new FlowLayout( FlowLayout.LEFT ) );
    tfRepD = new TextArea(10,70);
    pRepD.add( tfRepD );

    pUI = new Panel();
    pUI.setLayout( new BorderLayout() );
    pUI.add( "North", pNum );
    pUI.add( "Center", pConv );
    pUI.add( "South", pRepD );

    setLayout( new BorderLayout() );
    add( "North", pUI );
    add( "Center", new Panel() );
  }

  public String longBits( long n )
  {
    long mask = 1L << 63;

    StringBuffer txtBuf = new StringBuffer();

    while( mask != 0 ) {
      String bit = ( ( mask & n ) != 0 ) ? "1" : "0";
      txtBuf.append( bit );
      mask >>>= 1;
    }
    txtBuf.insert( 12, ' ' );
    txtBuf.insert( 1, ' ' );
    return txtBuf.toString();
  }

  public String intBits( int n )
  {
    int mask = 1 << 31;

    StringBuffer txtBuf = new StringBuffer();

    while( mask != 0 ) {
      String bit = ( ( mask & n ) != 0 ) ? "1" : "0";
      txtBuf.append( bit );
      mask >>>= 1;
    }
    txtBuf.insert( 9, ' ' );
    txtBuf.insert( 1, ' ' );
    return txtBuf.toString();
  }

  public boolean handleEvent( Event evt )
  {
    if( evt.id == Event.ACTION_EVENT ) {
      if( ( evt.target == bConv ) || ( evt.target == tfNum ) ) {
        String txtNum = tfNum.getText();
        Double D = new Double( 0.0 );
        Float F = new Float( 0.0F );
        try {
          if( txtNum.equalsIgnoreCase( "Inf" ) ) {
            D = new Double( Double.POSITIVE_INFINITY );
            F = new Float( Float.POSITIVE_INFINITY );
          } else if( txtNum.equalsIgnoreCase( "-Inf" ) ) {
            D = new Double( Double.NEGATIVE_INFINITY );
            F = new Float( Float.NEGATIVE_INFINITY );
          } else if( txtNum.equalsIgnoreCase( "NaN" ) ) {
            D = new Double( Double.NaN );
            F = new Float( Float.NaN );
          } else {
            D = new Double( txtNum );
            F = new Float( txtNum );
          }
          long n = Double.doubleToLongBits( D.doubleValue() );
          String txtBits;
          String txtExp;
          int e;
          txtBits = longBits( n );

          tfRepD.setText( "" );
          tfRepD.appendText( "IEEE754 Double Bits:\n" );
          tfRepD.appendText( txtBits );

          txtExp = txtBits.substring( 2, 13 );
          e = Integer.parseInt( txtExp, 2 );
          tfRepD.appendText( "\nExponent= " + e + " (" + txtExp + ")\n" );
          tfRepD.appendText( "Exponent-1023= "+(e-1023) );
          tfRepD.appendText( "\n" );

          int i = Float.floatToIntBits( F.floatValue() );
          txtBits = intBits( i );
          tfRepD.appendText( "\nIEEE754 Float Bits:\n" );
          tfRepD.appendText( txtBits );

          txtExp = txtBits.substring( 2, 10 );
          e = Integer.parseInt( txtExp, 2 );
          tfRepD.appendText( "\nExponent= " + e + " (" + txtExp + ")\n" );
          tfRepD.appendText( "Exponent-127= "+(e-127) );
          tfRepD.appendText( "\n" );
        } catch( NumberFormatException nfex ) {
          tfRepD.setText( "Number Format Wrong!!!" );
        }
      }
    }
    return super.handleEvent( evt );
  }
}
