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

methods to show Bits???  RSS feed

 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,
Is there a method that can show the bits of say an integer like
00000000 00000000 00000000 011101100 ???
just out of curiosity
I have looked through my api which is for Java 2 SDK version 1.3, i know i could get the latest one, but the distance learning course i am on gave me this version cause i have to do the 1.2 exam.
once completed i will move onwards and upwards!!!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the two-argument form of the static method Integer.toString(); pass "2" as the radix. This gives you the number as a base-2 (binary) string. Leading zeros will be missing, but if you need to you can add those back easily enough by appending them into a StringBuffer.
[ January 22, 2004: Message edited by: Ernest Friedman-Hill ]
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers Ernest,
I found what you were talking about and made a small program up to test this, it works, but why does it not show the every bit???
I take it if it is positive then all of the bits before what is displayed is 0's and if i get say -111000 then it means that all of the bits before what is displayed is 1's???
Davy
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Davy Kelly:
cheers Ernest,
I found what you were talking about and made a small program up to test this, it works, but why does it not show the every bit???

Well, because it's not trying to show the bits; it's just trying to show the number in binary. When you print a "1" in decimal, it never comes as "000001", right?

I take it if it is positive then all of the bits before what is displayed is 0's and if i get say -111000 then it means that all of the bits before what is displayed is 1's???

Yes. This comes from the way numbers are represented internally in Java (and by most other languages and most hardware), a system called "two's complement notation." See, for example, here for more info about it.
[ January 22, 2004: Message edited by: Ernest Friedman-Hill ]
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I liked the twos complement and twos compliment description.
thanks davy
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, note that the Integer class has a static toBinaryString(int) method that you could similarly use.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!