• 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

    • Great to see GPU monitoring support. Another nice improvement I noticed (that is not mentioned) is that the option to remove window border and titlebar has been added to taskbar entries.
    • I remember Google tested this same feature in Chrome Canary about SIX YEARS AGO, I posted about it on Reddit: https://old.reddit.com/r/chrom..._allows_users_to_drag_tabs/ . But it was scrapped for some reason I can't remember 🤷‍♀️
    • Everything this man does is just a distraction, hoping fools will look another way and ignore the dumpster fire this administration is.
    • MPC-BE 1.8.5 by Razvan Serea Media Player Classic - BE is a free and open source audio and video player for Windows. Media Player Classic - BE is based on the original "Media Player Classic" project (Gabest) and "Media Player Classic Home Cinema" project (Casimir666), contains additional features and bug fixes. The BE mod (Black Edition Mod) is a skinned version of Media Player Classic Home Cinema, much better looking than the plain old MPC. MPC-BE 1.8.5 changelog: AviSplitter Improved support for VP9 and AV1 video. MatroskaSplitter Improved support for VP9 and Apple ProRes video. Improved work with unstable sources. MpegSplitter Added support for AV1 video. MPCVideoDec Improved support for VP9 video. Added the ability to hardware decode slightly non-standard AV1 videos. Speeded up copying of video frames for CopyBack modes (optimizations from the LAV Filters project were used). MpcAudioRenderer Fixed crashes in some situations. Improved Push method. YouTube The default format is now VP9 720p + Opus. Fixed merging of saved VP9 and AAC streams using ffmpeg.exe. Added checks for availability of the URL being opened. Improved yt-dlp support. Added the ability to select the default language as the preferred audio track language. Player Fixed saving of video frame image with subtitles when using MPC Video Renderer. HLS M3U files are no longer processed as normal playlists, but are passed to the source filter as is. Added the ability to save the "Show milliseconds" setting. Added settings "Controls size" and "Use Blur Behind effect (Windows 10 and above)". Added OSD settings panel. Added a Debanding shader. Various interface fixes. Installer Updated MPC Video Renderer 0.9.9. Updated MPC Script Source 0.2.9. Updated Bulgarian translation (by jekovcar). Updated Romanian translation (by Andrei Miloiu). Updated Slovak translation (by PEPEK). Updated Japanese translation (by tsubasanouta). Updated Chinese (Simplified) translation (by wushantao). Updated German translation (by Klaus1189). Updated Turkish translation (by cmhrky). Updated Hungarian translation (by mickey). Updated Spanish translation (by IPeluchito). Updated Italian translation (by mapi68). Updated Korean translation (by Hackjjang). Updated Dutch and Chinese (Traditional) translation (by beter). Updated libraries ffmpeg n7.2-dev-2722-gfb65ecbc9b; Little-CMS git-lcms2.17-31-g9ac94ea; MediaInfo git-v25.04-121-gf842d2fea; ZenLib git-v0.4.41-76-gf5f6974. Download: MPC-BE 64-bit | Portable MPC-BE 64-bit | ~16.0 MB (Open Source) Download: MPC-BE 32-bit | Portable MPC-BE 32-bit | ~13.0 MB Link: Media Player Classic - BE Home Page Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • That would have been a so much better UX that what it is right now. I know that after a few moment of trying/failing to recognize you in the dark it goes back to PIN selection. But if the light sensor would detect the dark light, showing the PIN field while continuing for a brief moment to register your face would work better.
  • Recent Achievements

    • 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
    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      695
    2. 2
      ATLien_0
      274
    3. 3
      Michael Scrip
      215
    4. 4
      +FloatingFatMan
      187
    5. 5
      Steven P.
      145
  • Tell a friend

    Love Neowin? Tell a friend!