Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top