I need to get a string from the console that is given in hex form like this: 3e900000.
Then I need to convert it into bit so I can do bit wise thigns too it to generate the following out put:

3e900000 ==> 2^-2(1 + 2^-3)

The above is a factored way of showing IEEE 754 for 32bit floating point decimal numbers.

I know that I can use String args[] to get the console input. So I would be using args[0] . No need for error checking on this one.

I can do the problem but I'm stuck on being able to easily get a real form of 32 bits of binary. Im not allowed to use floating point variables in any of the intermediate steps. I also need to avoid the use of Strings where I can, avoid arrays(for temp storage) and avoid Math.pow().

The point of the problem is not to deal with java or how to convert stirngs to what ever but the binary manipulation using bitwise functions and bit shifts. Like I said, I can do the bits part, Im just stuck on hex string to real full 32 bit binary no trimming leading 0 or stuff like that. it has to be in a form that can be bit masked.

Recommended Answers

All 4 Replies

Float.parseFloat(String value, int radix);

See the API docs.

i used Long.parseLong(String value, int radix); there is not Float.parseFloat(String value, int radix);

I seem to have a hard time searching the API to find what I need.

I don't know what I was thinking of, I meant Long, of course. ;-)

here is the code. enter a valid 8 digit hex IEEE 754 string on commandline and you get the factored version in terms of powers of base two.

public class Fexpand
{
  public static void main(String[] args)
  {
    long hex = Long.parseLong(args[0], 16);

    // shift out every thing but the sign bit that is at 32 bit mark.
    long sign = hex >> 31;

    // returns 1 if sign bit is 1, else 0.
    long sign_bit = sign & 1;


    // shift to gain exponent value. shifting out the fractional part
    long binary_exponate = hex >> 23;

    // bit mask removes sign bit
    // 011111111
    long no_sign_bit_exponate = binary_exponate & 255;


    long exponent = no_sign_bit_exponate - 127;

    if (sign_bit == 1)
      System.out.print("-");

    System.out.print( args[0] + " ==> 2^" + exponent + "( 1 ");

    // bit mask 11111111111111111111111
    // test to see if there is any farctional part.
    if ((hex & 8388607) != 0)
    {

      for (int i = 1; i < 24; i++)
      {
        // bitmask 10000000000000000000000
        if ((hex & 4194304) == 4194304)
        {
          System.out.print("+ 2^-" + i + " ");
        }
        hex = hex << 1;
      }

    }
    System.out.println(")");
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.