• 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

    • 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!