1

Let's say I have a floating number in java. How can I print it in it's binary form? Is there a simple method to use in the java library? I am talking about IEEE 754 floating point. Or would I have to write it from scratch?

I'd also like to print it out.

munchschair
  • 1,593
  • 3
  • 19
  • 43

1 Answers1

3
Integer.toBinaryString(Float.floatToIntBits(FloatNumber)); 

Example:

class FloatToBinary { 
      public static void main( String ... args ) { 

      int BitRep =   Float.floatToIntBits(12345.12346f); //Bit Representation of the Float
      System.out.println(BitRep); //1178657918

      String BinString = Integer.toBinaryString(BitRep); //Binary representation in String
      System.out.println(BinString); //1000110010000001110010001111110



             }
        }

EDIT

Looks like an identical question has already been answered: how to convert a string of binary values to a float and vice-versa

Community
  • 1
  • 1
tutak
  • 1,120
  • 1
  • 15
  • 28