32
\$\begingroup\$

Given any unsigned 16 bit integer, convert its decimal form (i.e., base-10) number into a 4x4 ASCII grid of its bits, with the most-significant bit (MSB) at the top left, least-significant bit (LSB) at bottom right, read across and then down (like English text).

Examples

Input: 4242

+---+---+---+---+
|   |   |   | # |
+---+---+---+---+
|   |   |   |   |
+---+---+---+---+
| # |   |   | # |
+---+---+---+---+
|   |   | # |   |
+---+---+---+---+

Input: 33825

+---+---+---+---+
| # |   |   |   |
+---+---+---+---+
|   | # |   |   |
+---+---+---+---+
|   |   | # |   |
+---+---+---+---+
|   |   |   | # |
+---+---+---+---+

Specific Requirements

  1. Input must be in decimal (base-10), however you may convert to binary any way you wish (including using language built-ins, if available).

  2. Output table format must match exactly. This means you must use the specific ASCII characters (-, +, and |) for the table grid lines as shown, each cell's interior is 3 characters, and true bits are represented by # while false is represented by a space ().

  3. Leading or trailing whitespace is not permitted. Final newline is required.

  4. Bit order must match the examples as described.

Allowances

  1. Input must be a base-10 number on the command line, standard input, or user input, but must not be hard-coded into your source code.

May the clearest shortest code win! :-)

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Related \$\endgroup\$
    – Sp3000
    Sep 18 '15 at 16:38
  • 2
    \$\begingroup\$ The first sentence sounds confusing to me, where it says "convert its decimal form". Based on the rest of the post and the example, it looks like the input is given in decimal form, but you have to convert the binary form of the value into a grid. \$\endgroup\$ Sep 18 '15 at 16:44
  • 1
    \$\begingroup\$ @RetoKoradi you are essentially correct, but the question does require you to convert a decimal number into a binary grid. There is no explicit requirement to ever work with a binary number, only a likely implementation detail. \$\endgroup\$ Sep 18 '15 at 17:53
  • \$\begingroup\$ Does writing a function with the base-10 number as the function argument count as user input? \$\endgroup\$
    – Alex A.
    Sep 18 '15 at 18:12
  • 2
    \$\begingroup\$ Since you say that the given number is an "unsigned 16 bit integer", it is by definition in binary form. When I first read this, it actually sounded like the input would be given in binary form. It all becomes clear towards the end. But at least for me, the first paragraph really doesn't capture the problem at all. \$\endgroup\$ Sep 18 '15 at 18:20

31 Answers 31

1
2
0
\$\begingroup\$

Pip, 85 bytes

a:TBa
(("   "X16-#a).aR0"   "R1" # "<>3JW'|<>16J'|<>17JWn."+---+---+---+---+".n)@<-20

Does a straightforward bunch of splits and replacement to get the final grid.

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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