Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Outputing Binary Numbers  RSS feed

 
Joe Hoppe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to output binary numbers to the standard stream. The problem is that I want to display them in the mathematically correct format.
Using 7.5 as a decimal example, to display it as:
111.1
Do I have to write my own method to convert these, or is there a simpler way?
Thanks,
Joe Hoppe
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Joe!
If you're not already familiar with it, you'll definitely want to get to know the J2SE API Specification.
Notice that the Double class has a method or two that you might find useful - doubleToLongBits and doubleToRawLongBits. If you want to see the bits that either of these outputs, try a simple example such as the following.
If you understand the IEEE 754 floating point bit pattern, then you should be able to figure something out.
Otherwise, it wouldn't really be all that much much to write a simple algorithm to convert the number "by hand".
Let us know how you make out.
Good luck.
 
Joe Hoppe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a bad idea about converting from from the IEEE 754 Standard... I didn't think of that.
But I decided to do it manually. On the positive side (opposing my wasting time), it gives me a little greater control over the output.
Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!