29
6
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
Input must be in decimal (base-10), however you may convert to binary any way you wish (including using language built-ins, if available).
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 ().
Leading or trailing whitespace is not permitted. Final newline is required.
Bit order must match the examples as described.
Allowances
- 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! :-)
1Related – Sp3000 – 2015-09-18T16:38:30.773
2The 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. – Reto Koradi – 2015-09-18T16:44:56.133
1@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. – type_outcast – 2015-09-18T17:53:38.300
Does writing a function with the base-10 number as the function argument count as user input? – Alex A. – 2015-09-18T18:12:52.520
2Since 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. – Reto Koradi – 2015-09-18T18:20:06.837
@RetoKoradi OK, I understand the confusion now. You say "it is by definition in binary form", but in computing, saying something is an "N-bit integer" simply means it is an integer with a width of N bits (and usually the width is a common power of 2, such as an 8-bit integer (octet, byte), 16-bit, 32-bit, 64-bit). The integer size has nothing to do with its representation. A 16-bit integer can be expressed in any real base, including binary and decimal. See for example https://en.wikipedia.org/wiki/32-bit
– type_outcast – 2015-09-18T18:36:34.633@AlexA. Yes, that's acceptable – type_outcast – 2015-09-18T18:38:04.470
Then in the case of a function, can output be returned or must it be printed to STDOUT? – Alex A. – 2015-09-18T18:50:08.173
@AlexA. I was thinking printed output, as that seems to be the norm, but I don't see how allowing returned value instead of STDOUT would invalidate anyone's solution or give anyone an unreasonable advantage, so I'm inclined to allow either. What do you think? – type_outcast – 2015-09-18T20:24:26.263
In javascript
return
is better than the unreasonableconsole.log
. But in javascript and many other languages you can avoid the return token at all in many cases. – edc65 – 2015-09-18T20:32:00.720