Common Lisp, 41 39 34 32
2+5=7 bytes removed thanks to PrzemysławP.
I am using the FORMAT
function function, for printing a number as binary with padding and separators, called with the magic number 8800979740570 (a.k.a. 8012345679A in base 16):
CL-USER> (format t"~,,,4:b"8800979740570)
1000,0000,0001,0010,0011,0100,0101,0110,0111,1001,1010
I used to do the same with 123456789A, but as noted by PrzemysławP, the order does not matter and we can save some bytes. This is because with the original order, the leading zeros would be done as padding and would not be separated into groups of 4 digits; here, by starting with 1000, we don't need any padding. In addition to that, I also removed the mincol argument (53) because I don't need to specify the size of the number anymore.
Formatted output of integers
The documentation for format (see above) is detailed for ~D
(decimal base) but the same applies to other ones (hex ~X
, binary ~B
, octal ~O
, custom ~R
). Note that for floats there are other arguments.
The most general format is ~mincol,padchar,commachar,commaintervalD
, but note that the two last arguments, commachar and commainterval are only meaningful if D
has a colon-modifier :D
, which by default separate digits by groups of 3 (commainterval is a custom value, instead of 3), inserting the commachar character between such groups, by default a comma.
The mincol argument is one that is found in other format directives and specifies the horizontal space allocated for the thing being printed. When the actual size required is smaller, a padding might be applied (left or right, or both for some directives). The padding fills the remaining space with padchar, a character. When the actual size is bigger, the layout might not be pretty but the object is printed fully.
More precisely (from the above link):
~D
Decimal. An arg, which should be an integer, is printed in decimal
radix. ~D
will never put a decimal point after the number. ~mincolD
uses a column width of mincol; spaces are inserted on the left if the
number requires fewer than mincol columns for its digits and sign. If
the number doesn't fit in mincol columns, additional columns are used
as needed.
~mincol,padcharD
uses padchar as the pad character instead of space.
The @ modifier causes the number's sign to be printed always; the
default is to print it only if the number is negative.
The : modifier
causes commas to be printed between groups of three digits
the third
prefix parameter may be used to change the character used as the
comma. Thus the most general form of ~D is ~mincol,padchar,commacharD.
X3J13 voted in March 1988 (FORMAT-COMMA-INTERVAL) to
add a fourth parameter, the commainterval. This must be an integer; if
it is not provided, it defaults to 3. This parameter controls the
number of digits in each group separated by the commachar.
By extension, each of the ~B, ~O, and ~X directives accepts a
commainterval as a fourth parameter, and the ~R directive accepts a
commainterval as its fifth parameter.
Padding and separators
Note that the padding characters are not grouped, as explained above. For example, the zeros in front of the numbers are not separated:
CL-USER> (format t "~10,'0:d" 12345)
000012,345
... there is no easy way to combine format parameters to output:
000,012,345
... that's why I first added the zeros and a comma explicitly in the answer. This is not necessary anymore.
Yes, spaces, commas, a comma and then a space or a "\r\n" equivalent in your chosen language. – Shaun Bebbers – 2017-02-15T22:29:13.133
No sorry as that looks like 4 individual zero digits and not a 4-bit wide binary number. – Shaun Bebbers – 2017-02-15T22:33:53.507
Not that I'm really sure to write such an answer, but would it be OK to output some extra nibbles in addition to the 11 required ones? – Arnauld – 2017-02-15T23:12:43.633
4They're nibbles, not nybbles. – 0WJYxW9FMN – 2017-02-16T22:08:25.823
Not according to the Commodore 64 Programmers reference guide – Shaun Bebbers – 2017-02-16T22:13:36.253
An example of how to do this in Commodore BASIC V2 is here: http://pastebin.com/2VTTMZgU using
– Shaun Bebbers – 2017-02-27T09:48:44.590DEF FN