The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Pls improv this code.

Last post 11-10-2011 9:23 AM by toshir0. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 09-29-2011 10:04 AM

    • Nagesh
    • Top 50 Contributor
    • Joined on 01-31-2011
    • Hyderabad, India
    • Posts 911

    Pls improv this code.

    Can you make this code shorter? 
     
    import java.util.*;
    import java.lang.*;
    
    class Main
    {
    	public static void main (String[ args) throws java.lang.Exception
    	{
    
     int n = 2;
     char[ b = new char[32];
     int pos = 31;
     int i = 0;
    
     while (i < 32)
     {
         if ((n & (1 << i)) != 0)
         {
      b[pos] = '1';
         }
         else
         {
      b[pos] = '0';
         }
         pos--;
         i++;
     }
     System.out.println(new String(b));
    	}
    }
    
  • 09-29-2011 10:21 AM In reply to

    Re: Pls improv this code.

     

     System.out.println(n.ToString());
    Ed.
    I'm going to look it up, for once. Here's what the docs say, I think:
    Integer.toBinaryString(n);
    or
    Integer.toString(n, 2);
     
    Bear in mind that I know jack diddly squat about Java. 

    boomzilla: I think the obvious answer is for everyone to just stop programming.

  • 09-29-2011 10:34 AM In reply to

    • Nagesh
    • Top 50 Contributor
    • Joined on 01-31-2011
    • Hyderabad, India
    • Posts 911

    Re: Pls improv this code.

    dhromed:
     System.out.println(n.ToString());

    Main.java:10: int cannot be dereferenced
    System.out.println(n.ToString());
                         ^
    1 error

  • 09-29-2011 10:39 AM In reply to

    Re: Pls improv this code.

    Nagesh:
    1 error
     

    I ninja'd my post. See above.

     

    If you take the literal code as you posted it, it's System.out.println("00000000000000000000000000000010");but I'm assuming it's not as useless as that, and that n is the input that comes form somewhere and that this code is supposed to convert to the binary string equivalent, for some infathomable reason.


    boomzilla: I think the obvious answer is for everyone to just stop programming.

  • 09-29-2011 1:51 PM In reply to

    • Nagesh
    • Top 50 Contributor
    • Joined on 01-31-2011
    • Hyderabad, India
    • Posts 911

    Re: Pls improv this code.

    dhromed:

    Nagesh:
    1 error
     

    I ninja'd my post. See above.

    If you take the literal code as you posted it, it's System.out.println("00000000000000000000000000000010");but I'm assuming it's not as useless as that, and that n is the input that comes form somewhere and that this code is supposed to convert to the binary string equivalent, for some infathomable reason.

    import java.util.*;
    import java.lang.*;
    
    class Main
    {
    	public static void main (String[ args) throws java.lang.Exception
    	{
    
     int n = 2;
     System.out.println(Integer.toBinaryString(n));
    	}
    }
    
    Now to put string pading in there.
  • 09-30-2011 6:14 AM In reply to

    • flop
    • Top 500 Contributor
    • Joined on 04-27-2007
    • Posts 127

    Re: Pls improv this code.

     Use ABCL (then you'll get Java bytecode), and just do

    (let ((n 2))
      (format T "~B" n))
    10

    Oh, you wanted string padding? Sure, how about some mayo with it?

    (let ((n 212))
      (format T "~17,'xB" n))
    xxxxxxxxx11010100
  • 10-03-2011 12:55 PM In reply to

    • Nagesh
    • Top 50 Contributor
    • Joined on 01-31-2011
    • Hyderabad, India
    • Posts 911

    Re: Pls improv this code.

    flop:

     Use ABCL (then you'll get Java bytecode), and just do

    (let ((n 2))
      (format T "~B" n))
    10

    Oh, you wanted string padding? Sure, how about some mayo with it?

    (let ((n 212))
      (format T "~17,'xB" n))
    xxxxxxxxx11010100

     

     

    Wow! Will download this and try thing out sometime. Thx.

     

  • 10-28-2011 3:03 PM In reply to

    Re: Pls improv this code.

    or consider java.util.Formatter ... printf-like formatting capabilities ... its been around since Java 1.5

    It should be able to zero-pad your output.

  • 10-28-2011 3:30 PM In reply to

    • Nagesh
    • Top 50 Contributor
    • Joined on 01-31-2011
    • Hyderabad, India
    • Posts 911

    Re: Pls improv this code.

    zelmak:

    or consider java.util.Formatter ... printf-like formatting capabilities ... its been around since Java 1.5

    It should be able to zero-pad your output.

    Thx for your input.

  • 11-10-2011 9:23 AM In reply to

    Re: Pls improv this code.

    Nagesh:
    zelmak:

    or consider java.util.Formatter ... printf-like formatting capabilities ... its been around since Java 1.5

    It should be able to zero-pad your output.

    Thx for your input.

    You two should meet on middleput.
    Ionian peculiar of Pervertiti in the technology of the process of the data
Page 1 of 1 (10 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems