• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a method of Integer class

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
I did not understand the usage purpose of toBinaryString()method.
If anyone know, please inform me.

Regards
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Purpose: the first sentence in the API for toBinaryStringg sums it up:

Returns a string representation of the integer argument as an unsigned integer in base 2.

Usage: have you tried using it? Did it do what you expected?
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I do not understand why people use it.I do not see any advantages of its usage.If you disagree with me , please inform me.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you use instead?
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the purpose of it???
Would you mind explaining???
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, I still do not understand its purpose.
Would you help me by explaining??
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its purpose is to return a string representation of the integer argument as an unsigned integer in base 2. (That sentence may seem familiar ) Are you asking why would anyone ever need this method? Suppose you are using an int as a bit mask. (Take a look at InputEvent's getModifiers or getModifiersEx method for an example.) Often you want to see the bit pattern in a mask to help you understand what is happening, hence the need for toBinaryString.
[ January 26, 2006: Message edited by: Jeff Albrectson ]
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for quick replies.
I also wonder that why it is not consist of 32 bits instead of a few bits.

Regards
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Integer (and int) are 32 bit representations. Long, too, has a toBinaryString and it is for 64 bit representations. Are you looking for some specific functionality? Is there some binary operation you are trying to do?
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I only wonder.

Can you make this more clear by giving a simple example??

Often you want to see the bit pattern in a mask to help you understand what is happening


 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Beyond this, it sounds like you could benefit from finding a good introductory book and reading about binary representation. Here is a tutorial about bitwise operators, but I think it may be too advanced for you at this point: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 10 kinds of people in the world: Those who understand binary, and those who don't.
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you attempt to say the same thing again, you can make sure that something will change in your life!!!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Richards:
There are 10 kinds of people in the world: Those who understand binary, and those who don't.



LOL! Ain't that the truth!

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mert �zkaya:
Hi everyone,
I did not understand the usage purpose of toBinaryString()method.
If anyone know, please inform me.

Regards



The purpose of this method is to display an int value in its binary representation. Personally, I don't use this very often since I find the hexadecimal representation more useful when I am dealing with ints at the bit level. If you need more information, you should google for "number systems" and "twos compliment" to understand how integer values are represented in binary.

Layne
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are asking why this method exists, it's because it comes in handy sometimes. Bit masks are used for many low-level things. it's nice to be able to see the thing easily. perhaps this method would be used in debugging to see if you have your mask set right.

perhaps you want to use it to simulate a binary clock.

perhaps you are using it to write a binary calculator for a middle school math class...

perhaps it is something you will never use. But it was once decided that enough people need to use it often enough to just make it part of the api.

and if you ever DO need it, you'll be glad it's there.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic