• 0

[java]Converting integer to signed binary?


Question

9 answers to this question

Recommended Posts

  • 0
  Quote
String s = Integer.toString(i,2); // the 2 is what converts it to binary

System.out.println(s);

That still doesn't work because the function does not handle negative numbers. It instead puts a '"-" sign in front of the string.

  • 0

When I learned to convert integers to their 2's complement notation, I was taught to reverse the bits then add 1.

So could you do the following:

1) take an integer and convert it to bytes

2) go through each bit and reverse them (0->1 and 1->0)

3) add 1 to this "flipped bit" number

4) your result is that number in 2's complement notation

Now I realise there are probably much easier methods available to do this, this is just my first thoughts on the subject

  • 0
  Quote
Integer.toBinaryString()

"For negative numbers it will return a 32 bit two's compliment representation of that number."

Where does it say that? I'm looking at the java.sun.com API specification and this is what is said
  Quote
Returns a string representation of the integer argument as an unsigned integer in base 2.

The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s. If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The characters '0' ('\u0030') and '1' ('\u0031') are used as binary digits.

Nowhere does it says it will return a 32 bit two's complement representation! Can you tell me where you got that?

  • 0

Google Groups

class Test{
 public static void main(String args[]){
  System.out.println(Integer.toBinaryString(1));
  System.out.println(Integer.toBinaryString(-1));
  System.out.println(Integer.toBinaryString(256));
  System.out.println(Integer.toBinaryString(-256));
  System.out.println(Integer.toBinaryString(0));
  System.out.println(Integer.toBinaryString(31337));
  System.out.println(Integer.toBinaryString(-31337));
 }
}

C:\>javac Test.java

C:\>java Test
1
11111111111111111111111111111111
100000000
11111111111111111111111100000000

111101001101001
11111111111111111000010110010111

C:\>

As advertized, gives the 32-bits 2-complement when used with negative numbers.

  • 0

They just use different words to explain the 2-complement.

"The unsigned integer value is the argument plus 2^32 if the argument is negative; otherwise it is equal to the argument."

ie if the argument you pass to toBinaryString() is negative, it does += 2^32 before converting it to binary... which is exactly what a 2-complement is.

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

    • No registered users viewing this page.
  • Posts

    • Watchdog finds Microsoft guilty of confusing advertising when it comes to Copilot by Usama Jawad While Microsoft is advocating for Copilot usage in both personal and enterprise environments as much as possible, it arguably hasn't made as big of a splash as it wanted. Now, the U.S. watchdog National Advertising Division (NAD) has criticized Microsoft for some of its advertising practices when it comes to Copilot. In a recent blog post spotted by The Verge, NAD noted that Copilot does possess the functionalities that Microsoft expressly claims it does in enterprise use cases. This includes capabilities like summarization of content, generating text, and creating outlines for PowerPoint slide decks. While Copilot does have some limitations in this area, NAD thinks that they are pretty minor, so the lack of disclosure in this area doesn't really impact how most customers leverage Copilot. That said, the watchdog discovered some problems in Microsoft's messaging when it came to Business Chat. It believes that Redmond's universal and interchangeable use of Copilot for both the assistant and Business Chat can cause confusion among users, as they judge their functionalities. For example, NAD argues that while Copilot is quite fast at seamlessly generating documents in other applications, this is not the case with Business Chat, which requires more manual steps, and this is not abundantly clear in the advertising on Microsoft's website. Finally, NAD advised Microsoft to get rid of its claims regarding an increase in productivity and ROI because while the tech giant does have supporting studies to back its boasts, they do not provide a "good fit" for objective claims. It's also worth highlighting that the UK government recently did trial Copilot in work environments and discovered that participants did save 26 minutes per day on average. Still, it seems like this is not good enough for NAD. Interestingly, Microsoft did respond to NAD's advisory, saying that while it disagrees with some of the watchdog's findings, it has made changes to its advertising messages in order to comply with NAD's recommendations. Source: NAD via The Verge
    • My new emergency poo ticket holder
    • These are the same ignorant, gullible, cowardly fools who still believe that gods are real and that fictional White Jesus is coming back to Earth "any day now". It's a Venn-diagram of two almost perfectly concentric circles. In short, they'll buy anything you lie to them about as long as it makes them less afraid... Of things the rest of us know we don't have to be afraid of at all.
    • It isn't fair to shift all blame to the user. Yes, the user shouldn't have blindly trusted MS with all their data, but assuming the facts reported are correct, MS massively screwed up too. Both can be true.
    • How about with add up/down votes on posts instead? Then we could upvote posts that are True based on the facts and supported by evidence. And we could also downvote the ridiculous paranoid rantings of ignorant, gullible, cowardly people who still fall for ancient proven lies and obvious fearmongering scams behind such nonsense like white supremacy, Nazism, Trumpism, etc. I'm willing to give it a try. How about you?
  • Recent Achievements

    • Experienced
      dismuter went up a rank
      Experienced
    • One Month Later
      mevinyavin earned a badge
      One Month Later
    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
    • Veteran
      matthiew went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      699
    2. 2
      ATLien_0
      268
    3. 3
      Michael Scrip
      211
    4. 4
      +FloatingFatMan
      186
    5. 5
      Steven P.
      142
  • Tell a friend

    Love Neowin? Tell a friend!