• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

outputting a number in binary form

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering if there was an easy way to output an int or long in their binary form ie. the number 5 as 00000101?
Any suggestions?
------------------
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class java.lang.Integer has many methods for creating String representation of integer values as binary, hex, octal, etc.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example:
String s = Integer.toBinaryString(5);
System.out.println(s);
or
String s = Integer.valueOf(5,2); // 5 is the int to print base 2
 
Brett Delia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestions...I have tried both ways ans they work great.
I am actually going to resort to creating my own method for displaying the binary format for a number. I am head up a study group at work for Java Certification and I have come up with a 'homework' assignment for the engineers to do to get more familiar with bit shifting and the bit-wise operators. So what I am wanting is something that displays the binsary number including all bits for a specific data type, for instance...
an int 5 -> 00000000000000000000000000000101
whereas the methods above would show only 101
This way it is easier to get a grasp on what exactly the different bit-wise operaters are doing.
Thanks for the help...
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the DecimalFormat class to do exactly that.
so if you had:
DecimalFormat binFormat = new DecimalFormat("00000000");
and then just did something like
int sumBinNum = 101;
System.out.println("The binary number: " + binFormat.format(sumBinNum));
then you will get "The binary number: 00000101"
[This message has been edited by Tom Patrick (edited March 07, 2001).]
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brett Delia:
So what I am wanting is something that displays the binsary number including all bits for a specific data type, for instance...
an int 5 -> 00000000000000000000000000000101
whereas the methods above would show only 101


Take a look at Cat and Mouse Game with Bits
The routine that formats the bit output in the applet is



------------------
Hope This Helps:)
Carl Trusiak
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic