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

Pls improv this code.

Last post 04-25-2014 11:06 PM by flabdablet. 14 replies.
Page 1 of 1 (15 items)
Sort Posts: Previous Next
  • 09-29-2011 10:04 AM

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

    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. 

    In complex analysis, a meromorphic function on an open subset D of the complex plane is a function that is holomorphic on all D except a set of isolated points

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

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

    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.


    In complex analysis, a meromorphic function on an open subset D of the complex plane is a function that is holomorphic on all D except a set of isolated points

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

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

    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 148

    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 1,070

    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 1,070

    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
  • 11-17-2013 3:51 AM In reply to

    Re: Pls improv this code.

    Okay, so.

    • Your while loop uses the logic of a for loop, so why is it not a for loop? 
    • pos is entirely dependent on i , so you could write int pos = 31 - i on each iteration of the while loop (but not necessary at all if you use a string builder, which see)
    • Don't throw all exceptions
    • You don't need those imports, either.
    • Use a string builder to build a string
    • give your class an indicative name
    • Delegate conversion of integer to string to its own method for the sake of making the code reusable
    • n is never modified so should be declared final
    • mismatched square brackets

    class IntegerConverter {
            static final int n = 2;
            public static void main (String[ args) {
                    String toBePrinted = convertIntTo32BitString(n);
                    System.out.println(toBePrinted);
            }
            public static String convertIntTo32BitString(int input) {
                    StringBuilder output = new StringBuilder();

                    for(int i=0;i < 32;i++) {
                            if ((n & (1 << i)) != 0) {
                                    output.insert(0,'1');
                            } else {
                                    output.insert(0,'0');
                            }
                    }
                    return(output.toString());
            }
    }

     

  • 03-22-2014 7:24 AM In reply to

    Re: Pls improv this code.

    Hmm, closer examination of that code would suggest input is being thrown away - the subsequent n should presumably be an input.

    Want conciseness?

    object IntegerConverter extends App {

      val n = 2

       def convertIntTo32BitString(input :Int) = (Range(0,32) :\ "")((b,a) => a+(if ((input & (1 << b)) != 0) '1' else '0'))

       println(convertIntTo32BitString(n))

    }

  • 04-24-2014 1:02 PM In reply to

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

    Re: Pls improv this code.

     Pretty nice!

     

    scuffleball:

    Hmm, closer examination of that code would suggest input is being thrown away - the subsequent n should presumably be an input.

    Want conciseness?

    object IntegerConverter extends App {

      val n = 2

       def convertIntTo32BitString(input :Int) = (Range(0,32) :\ "")((b,a) => a+(if ((input & (1 << b)) != 0) '1' else '0'))

       println(convertIntTo32BitString(n))

    }

     

     

  • 04-25-2014 1:44 AM In reply to

    • Ben L.
    • Top 10 Contributor
    • Joined on 12-21-2010
    • Inventor of the 186-hour work week
    • Posts 3,607

    Re: Pls improv this code.

    user@vensa:~/ruby$ irb
    2.1.1 :001 > "%032b" % 2
     => "00000000000000000000000000000010" 
    2.1.1 :002 > "%032b" % 212
     => "00000000000000000000000011010100"
  • Morbs is the smartest!
  • 04-25-2014 11:06 PM In reply to

    Re: Pls improv this code.

    If you're walking a mask bit over all the bits in a word, there's no need to use a separate index for loop control. Just keep shifting the mask until its 1-bit falls off the end:
    for (int mask = 1; mask != 0; mask <<= 1) {
         output.insert(0, (n & mask) == 0? '0': '1');
    }
    
Page 1 of 1 (15 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems