18
\$\begingroup\$

Background:

You have been given an assignment to convert base 10 numbers to base 2 without using any premade base conversion functions. You can't use any imported libraries either.

Problem:

Convert an input string from base 10 (decimal) to base 2 (binary). You may not use any premade base conversion code/functions/methods, or imported libraries. Since this is , the shortest answer in bytes will win.

Input will be anything from -32768 to 32767 (include sign byte handling in your code)

\$\endgroup\$
6
  • 3
    \$\begingroup\$ Q: what does "sign byte handling" mean - shall I output "-xxxx" for a negative number? Then some of us are wrong, incl. me, as I output "11...11" for -1 (aka as unsigned) \$\endgroup\$
    – blabla999
    Commented Feb 16, 2014 at 2:19
  • \$\begingroup\$ Sign byte handling - the MSB of signed variables controls if they are negative \$\endgroup\$
    – TheDoctor
    Commented Feb 16, 2014 at 3:12
  • 1
    \$\begingroup\$ sure, but do I have to >print< them as sign '-' followed by magnitude? \$\endgroup\$
    – blabla999
    Commented Feb 16, 2014 at 3:14
  • \$\begingroup\$ @blabla999 - No you don't \$\endgroup\$
    – TheDoctor
    Commented Feb 16, 2014 at 3:34
  • 5
    \$\begingroup\$ the MSB of signed variables controls if they are negative - that sounds like sign bit, however as the range -32768..32767 suggests, you want 2's complement. So which do you want?.. \$\endgroup\$
    – mniip
    Commented Feb 16, 2014 at 12:03

32 Answers 32

1
2
0
\$\begingroup\$

Java 10, 80 71 57 bytes

n->{var r="";for(n=n<0?-n:n;n>1;n/=2)r=n%2+r;return n+r;}

-9 bytes due to a rule in the comments.. Negative base-10 inputs may return the positive/absolute base-2 value as output apparently.

Explanation:

Try it online.

n->{                   // Method with integer parameter and String return-type
  var r="";            //  Result-String, starting empty
  for(n=n<0?-n:n;      //  Transform the input to its absolute value
      n>1;             //  Loop as long as `n` is still >= 2:
      n/=2)            //    After every iteration: integer-divide `n` by 2
    r=n%2+r;           //   Prepend `n` modulo-2 to the result
  return n             //  Return the last `n`
         +r;           //  appended with the result
\$\endgroup\$
0
\$\begingroup\$

MATL, 15 17 bytes

t0<?16Ww+]`2&\t]x

Try it on MATL Online

TIO

(+2 bytes removing leading 0 for negative numbers, sign bit should be the first bit.)

Output on MATL Online should be read bottom-up (MSB is at the bottom).

The main part is pretty simple: `2&\t = while the value is greater than 0, divide by 2 and accumulate the remainders.

Handling negative numbers and giving them 2's complement representation was the tricky part. In the end I went with the "subtract from \$ 2^N \$" method of getting a number's two's complement. Since we're only required to handle values upto -32768, for negative numbers the code creates \$ 2^{16} = 65536 \$ with 16W, adds the input to that (eg. 65536 + (-42)), which gives something MATLAB sees as a positive number but represents the input's signed binary representation in 16-bit form.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.