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)); } }
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.
dhromed: System.out.println(n.ToString());
Main.java:10: int cannot be dereferenced System.out.println(n.ToString()); ^1 error
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.
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)); } }
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
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.
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.
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.
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.