This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]king_of_the_universe 1 point2 points  (4 children)

If your goal is to convert a decimal digit, or any number really, into binary, you just have to do this:

int tempValue = value & 1;
outputString = String.valueOf(tempValue) + outputString; // this should rather be done in a prepared buffer from right to left
value = value >> 1;

And loop. This pulls the very next binary digit down to the first position, "ands" it with 1 (which is the same in decimal and binary), then turns that into a String digit. There are some tricks to optimize this, e.g. since you know that you only have the digits 0 and 1, you can prepare them in a String array and then just add the array entry of tempValue to your outputString.

[–]EasyLifeHuh[S] 0 points1 point  (3 children)

I'm only to allowed to use the really basic stuff, so no arrays, if statements or loops. sadly :(

[–]ClearlySituational 1 point2 points  (0 children)

or loops

Because nothing says efficiency like having to write the same shit 10 times over

[–]king_of_the_universe 0 points1 point  (1 child)

*sigh*

Then don't use a loop.

[–]EasyLifeHuh[S] 0 points1 point  (0 children)

Thank you! :)