• 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

    • Maybe the site will get out of dabbing into Identity Politics.
    • ASUS TUF Gaming A16 (2024): A capable gaming laptop with AI for $300 less by Paul Hill If you are in the market for a mid-range gaming laptop packed with AI features, check out the ASUS TUF Gaming A16 2024 laptop, which is now discounted by 19% to just $1,299. The list price of this laptop is $1,599.99, so you’re saving $300 by taking advantage of this deal. Inside are the AMD Ryzen AI 9 HX 370 processor, which supports Windows AI features such as Recall, and the Nvidia GeForce RTX 4060 GPU, which offers solid gaming performance at 1080p. What you get: Features and performance for gamers The ASUS TUF A16 includes a 16-inch display with a 16:10 aspect ratio. Its refresh rate is 165Hz to minimize lag, it supports Nvidia G-Sync, and it has a 2.5K resolution. Inside the device, we have the AMD Ryzen AI 9 HX 370 processor, Nvidia GeForce RTX 4060 GPU, 16GB of LPDDR5X RAM, and a 1TB PCIe Gen 4.0 SSD. A nice thing ASUS has taken into consideration with this laptop is durability. It is MIL-STD-810H certified, meaning it has passed durability tests. While the laptop is discounted, it’s not cheap, so knowing it’s durable is a plus. In terms of cooling, it features Arc Flow Fans, four exhaust vents, five head pipes, and an anti-dust filter. Combined, these will keep the laptop running cool, providing better gaming performance. As for sound, the A16 features Dolby Atmos, Hi-Res Audio, and two-way AI noise cancellation. All of this will make your gaming sessions more immersive, and the noise cancellation will ensure you can hear your teammate properly. The laptop runs Windows 11 and includes 3 months of PC Game Pass, so you can play games right away, and save some money given the rising cost of games. Who should buy this laptop? Buyers can expect solid performance from this laptop for its price point. With the fast SSD, AI-capable CPU, and decent graphics card, you can expect to get most school or work tasks done easily, and it should capably handle games up to 1080p. The cooling features are nice because they will prevent any performance degradation, and the MIL-STD-810H rating offers peace of mind related to durability. If you are seeking the cutting edge of gaming performance, this laptop may not be for you. Additionally, if you aren’t much of a gamer or a creator, this laptop could be overkill, and you’d get away with buying something cheaper. If you want to pick it up, check out the buying link below. ASUS TUF Gaming A16: $1,299 (Amazon US) / MSRP $1,599.99 This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • After Elon has been high as a kite IN the oval office on video, I don't think anyone can mention Hunter and it mean anything anymore. As far as I'm concerned, Elon wiped the Hunter laptop issue away. And Marge Traitor Green just used that laptop to show congress Hunter naked so that she could look at him more! That was horrible.
    • AI vibrators do the same thing now..... err wait....
  • Recent Achievements

    • Reacting Well
      Cole Multipass earned a badge
      Reacting Well
    • Reacting Well
      JLP earned a badge
      Reacting Well
    • Week One Done
      Rhydderch earned a badge
      Week One Done
    • Experienced
      dismuter went up a rank
      Experienced
    • One Month Later
      mevinyavin earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      695
    2. 2
      ATLien_0
      275
    3. 3
      Michael Scrip
      218
    4. 4
      +FloatingFatMan
      188
    5. 5
      Steven P.
      146
  • Tell a friend

    Love Neowin? Tell a friend!