• 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

    • The long-awaited Nothing Phone (3) is finally coming next month, launch date confirmed by Aditya Tiwari London-based consumer electronics brand Nothing is due to launch its latest flagship in 2025. The company dropped a new teaser for the Phone (3), revealing when the flagship device will be out on the market. Nothing Phone (3) will be unveiled during a live event on July 1 at 1:00 PM ET / 10:00 AM PT / 6:00 PM BST / 11:30 PM IST. It has already created a live event titled "Come to Play" on its official YouTube channel, for which you can add a reminder by clicking on the "Notify Me" button. Nothing CEO Carl Pei has previously dropped several details about the unreleased smartphone. He took part in a social media AMA earlier this year and said Nothing Phone (3) will arrive in the third quarter of 2025. Pei confirmed that Nothing Phone (3) will make its way to the US this time after a dry spell since 2023. However, he didn't specify whether the device will be sold directly or through the beta channel, which currently includes Phone (3a), Phone (3a) Pro, and CMF Phone 2 Pro. The US has been a rocky terrain for Nothing. Its first smartphone was made available through the beta channel, and the Nothing Phone (2) is the only smartphone from the company that has been widely available in the US. The Glyph interface featured on the back of Nothing smartphones has remained a differentiating factor from the start. However, the smartphone maker recently posted a 9-second video in which the Glyph lights on the back of a Nothing smartphone abruptly turn off. "We killed the Glyph Interface," the company said. It makes sense when you check out the Phone (3) teaser and see dot matrix-style lights being flashed in a pattern. Nothing released another teaser about a week ago, featuring the number 3 lit up as dot matrix LEDs. If you're looking for some trivia, Nothing product manager Raymond Zhu estimated in a Q&A video that the company would need to sell about 250,000 Phone (3) units to turn a profit. Answering another question, he added that their biggest weakness is "no one knows us", and the company is struggling to reach the masses without high marketing budgets. Let's wait to see what Nothing has in store for Phone (3) next month other than the new physical button. Speaking of the future of smartphones, the Nothing CEO believes that our entire software experience will eventually be condensed down to just one app.
    • As far as I can remember, no one has done a 4v4 before. 2v2? yes. 3v3? yes.
    • I just checked with Brave portable, the shield is disabled, as well as uBlock Origin Lite, no change to the Host file, and no pop-up when I connect to the site 😵! I think this might be the best option? I don't have a Paypal account, because I got rid of it, and I was fed up with all the phishing attempts, so I use my credit card. Do you take credit card payments? Because so far it's been safer, no phishing attempts.    
    • *facepalm* Good sir... holy smokes. ...anyways I assume Qanba and the rest will also produce options in the near future.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      403
    2. 2
      +FloatingFatMan
      179
    3. 3
      snowy owl
      174
    4. 4
      ATLien_0
      170
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!