• 0

[JAVA] Big Integer to Binary


Question

13 answers to this question

Recommended Posts

  • 0

BigInteger does contain a radix feature, allowing you to supply the base-radix number for the instance:

BigInteger myInt = new BigInteger("12");
String binaryString = myInt.toString(2);

The toString() method can take an integer argument to represent the radix value (the base-radix value, e.g. base-2, base-10, base-16 etc). When the method runs, it will convert the number into the binary representation, so the above example should output the myInt value as the binary representation of 12 = '11000'.

I haven't done java in a while (it's mainly because I hate it), so I could be wrong!

  • 0

You can also do the same thing to take a binary integer and make it a BigInteger:

String binaryString = "1010110101011010101010101101010101100101011010001010001100101110";
BigInteger bigInt = new BigInteger(binaryString, 2);

Untested, but according to the docs, it should work. That constructor has been there since J2SE 1.3 at least. :)

  • 0
  Primexx said:
care to explain?

I could, but Wikipedia explains it so much better - http://en.wikipedia.org/wiki/Balanced_ternary

Simply put, there are three values, like in normal (unbalanced) ternary: -1, 0 and 1, which are easily represented using '-', '0' and '+' respectively.

The idea is the same as in any base. Multiply the place value by the value at that place and do the same for the rest. Then add them up. Example:

+-0- (balanced ternary) to decimal:

+ x 3^3 = 1 x 3^3 = 1 x 27 = 27
- x 3^2 = -1 x 3^2 = -1 x 9 = -9
0 x 3^1 = 0 x 3^1 = 0 x 3 = 0
- x 3^0 = -1 x 3^0 = -1 x 1 = -1

(27) + (-9) + (0) + (-1) = 18 + -1 = 17

Strange? Not really. Also, there is no need for a sign bit, unlike binary. -+ is -3 + 1 = -2. However, if you thought of the '-' as a sign bit like it is in binary, then wouldn't it really be ------...---+ ? But then you're subtracting A LOT, not just denoting a sign. So techically, you can write -+ as 000000...00-+. The same is true for positive numbers as well. In other words, all values would be unsigned, and positive and negative values could be represented equally!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • A few things I am wondering about for S3: A.  What's Pike doo gonna look like this time around....yea I've watched the trailers & it looks poofed up, but still.... B. When will the unintelligible, physco-babbling Pelia go away (or at least learn how to speak English properly, without that crappy, slurred accent that sounds like she's on crack, Mary Jane, & LSD at the same time) ?  Hopefully Scotty's arrival will mean her departure is near.... C.  When do we get to see more of the stunningly gorgeous No. 1, preferably in regular/civilian clothes that show off her physical attributes better ? D. Is Spock EVER gonna get laid properly, human style ?  I feel certain that Christine could make any Vulcan-human horizontal bomp a thing to remember for a LONG time  E.  Can we PLEEEEEEZE get rid of the hatchet-head/buzz cut hair styles on Ortega & anyone else that has it....  But otherwise, as Pike says:  HIT IT !  
    • RIP Hotlips..... IMHO, her best scenes were the few where she dared to let her REAL feminine side show through from underneath all that crappy, worn-torn soldier facade that she had to keep up with...she instantly lit up every room or situation where she was featured !
    • Helium Converter 3.3.69.0 by Razvan Serea Helium Converter is a free Windows utility for converting audio files between formats such as MP3, FLAC, AAC, WMA, OGG, and WAV. It supports batch conversion, preserves or updates tag information, and offers features like volume normalization. With a simple interface, it's ideal for users who need to convert large music libraries quickly and efficiently while retaining metadata. Helium Converter key features: Supports file formats: MP3, MP4, FLAC, AAC, M4A, WMA, WAV, OGG, OPUS, APE.... Batch conversion for large music libraries Preserves and edits metadata (ID3, Vorbis Comments, etc.) Volume normalization to equalize loudness Album art extraction and embedding Drag-and-drop interface for quick file selection Adjustable encoding parameters (bitrate, sample rate, channels) Uses internal codecs for consistent performance Supports CUE sheets for split track conversion File renaming based on tags during export Unicode support for international file and tag names Logging of conversion processes for troubleshooting Multi-core CPU support for faster conversions Download: Helium Converter 3.3.69.0 | 25.1 MB (Freeware) Links: Helium Converter Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Since it's been quite a while since the last episode aired, would it be fair (or cruel) to refer to Peggy as a MILF, or even a SMILF ?  HAHAHAHAHAHA
    • Their computers are not gonna stop working in October
  • Recent Achievements

    • One Year In
      WaynesWorld earned a badge
      One Year In
    • First Post
      chriskinney317 earned a badge
      First Post
    • Week One Done
      Nullun earned a badge
      Week One Done
    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      172
    2. 2
      ATLien_0
      122
    3. 3
      snowy owl
      121
    4. 4
      Xenon
      117
    5. 5
      +Edouard
      93
  • Tell a friend

    Love Neowin? Tell a friend!