Counting in binary nybbles

20

This challenge is to output to your terminal, window, canvas or screen the numbers zero to 10 inclusive. Each outputted number must be shown as a 4-bit wide nybble, so zero must show as 0000 and so on.

You may separate each number outputted with a space, comma or carriage return. Smallest solution wins but the numbers can be displayed in any order you like as long as there are no repeating numbers in your sequence.

Entries in low-level binary languages need not worry about the comma or white space separators if it is not possible to output with commas or white spaces (i.e., the standard output is limited to binary only, or your solution is for an early computer kit such as the KIM-1 which has a limited digital display).

Shaun Bebbers

Posted 2017-02-15T22:20:11.827

Reputation: 1 814

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 DEF FN

– Shaun Bebbers – 2017-02-27T09:48:44.590

Answers

2

SmileBASIC, 26 bytes

FOR I=0TO&HA?BIN$(I,4)NEXT

12Me21

Posted 2017-02-15T22:20:11.827

Reputation: 6 110

16

MATL, 6 bytes

0:10YB

Try it at MATL Online

Explanation

0:10    % Create the array [0...10]
YB      % Convert this array to a binary string where each number is 
        % placed on a new row
        % Implicitly display the result

Suever

Posted 2017-02-15T22:20:11.827

Reputation: 10 257

15

05AB1E, 9 8 bytes

T         # push 10
 4ã       # cartesian product repeat with 4
   R      # reverse list
    T>£   # take the first 11 elements of the list
      »   # join by newline and display

Try it online!

Emigna

Posted 2017-02-15T22:20:11.827

Reputation: 50 798

10Wait... the Cartesian product of the digits of a number? That's just... – ETHproductions – 2017-02-15T22:47:33.330

13

JavaScript, 46 bytes

for(i=15;i++<26;)alert(i.toString(2).slice(1))

Why use a padding function when you can simply add 16 to each number and slice off the first binary digit?

ETHproductions

Posted 2017-02-15T22:20:11.827

Reputation: 47 880

9

Japt, 7 bytes

GôA,_¤Å

And here I was thinking Japt was doomed to be longer than every other golfing language...

Test it online!

Explanation

GôA,_¤Å  // Implicit: A = 10, G = 16
GôA      // Create the inclusive range [G...G+A].
    _    // Map each item Z to Z
     ¤   //   .toString(2)
      Å  //   .slice(1).
         // Implicit: output result of last expression

Normally commas can be removed in Japt, but this one is there because of a bug: _ normally means function(Z){Z, but for some reason the compiler thinks A_ means function(A,Z){Z.

ETHproductions

Posted 2017-02-15T22:20:11.827

Reputation: 47 880

Nice one. I got stuck at Aô_¤ – Oliver – 2017-02-16T01:06:15.613

8

Bash + GNU utils, 26

  • 4 bytes saved thanks to @Dennis
seq -w 0 1010|sed /[2-9]/d

Try it online.

Digital Trauma

Posted 2017-02-15T22:20:11.827

Reputation: 64 644

1seq -w 0 1010 should work. – Dennis – 2017-02-16T01:07:11.423

@Dennis Thanks - I don't remember using the -w option to seq before. – Digital Trauma – 2017-02-16T01:23:52.950

7

Bash + Unix utilities, 29 26 bytes

dc -e2o8927II^*8/p|fold -4

Try it online!

This is the same length as @DigitalTrauma/@Dennis's solution, but uses a completely different method.

Output is:

1010
0010
0110
0001
1001
0101
0100
0111
0011
1000
0000

(Any order is allowed.)


Pure Bash, 34 bytes

echo 0{0,1}{0,1}{0,1} 10{00,01,10}

Try the pure Bash version online!

Output is:

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010

Mitchell Spector

Posted 2017-02-15T22:20:11.827

Reputation: 3 392

7

J, 6 bytes

#:i.11

Thanks to miles for cutting it down to 6 bytes!

Blocks

Posted 2017-02-15T22:20:11.827

Reputation: 141

#:i.11 should work just as well – miles – 2017-02-24T01:58:52.560

I'm not sure this is valid, as per the answer to a now deleted comment.

– Adám – 2017-03-01T05:57:02.163

@Adám I can't view it. Could you please explain why it's not vaild? – Blocks – 2017-03-01T07:20:53.023

Because it generates a n×4 Boolean array, which prints as digits with spaces in-between. But the comment seems to imply that spaces are not allowed inside the binary numbers. – Adám – 2017-03-01T07:22:20.073

6

Python 3.6, 36 35 bytes

i=11
while i:i-=1;print(f"{i:04b}")

-1 byte thanks to @JonathanAllan

Python 3.5 and earlier:

i=11
while i:i-=1;print("{:04b}".format(i))

Try it online!

ovs

Posted 2017-02-15T22:20:11.827

Reputation: 21 408

1i=11 (new line) while i:i-=1;print(f"{i:04b}"), for 35. – Jonathan Allan – 2017-02-16T01:44:41.867

6

Jelly, 7 bytes

2Bṗ4ṫ6Y

Try it online!

(5 bytes if trailing lines of nybbles are allowed, 2Bṗ4Y)

How?

Prints in descending order.

2Bṗ4ṫ6Y - Main link, no arguments
2B      - 2 converted to binary -> [1,0]
  ṗ4    - Cartesian 4th power -> [[1,1,1,1], [1,1,1,0], ..., [0,0,0,0]]
                            i.e.  16       , 15         ..., 0
    ṫ6  - tail from 6th item  -> [[1,0,1,0], [1,0,0,1], ..., [0,0,0,0]]
                            i.e.  10       , 9        , ..., 0
      Y - join with line feeds
        - implicit print

An alternative 7-byter is 2ṗ4Ịṫ6Y, the [1,0] is replaced with [1,2] and is the "is insignificant" monad (abs(z)<=1), converting 2s to 0s.

Jonathan Allan

Posted 2017-02-15T22:20:11.827

Reputation: 67 804

4

CJam, 12 bytes

B{G+2b1>}%N*

Try it online!

Explanation

The Cartesian power approach would have been my choice, but was already taken.

So this generates numbers from 0 to 10, and for each it adds 16 and converts to binary. Adding 16 ensures that the required leading zeros are produced, together with an extra leading one which is removed.

B             e# Push 11
 {      }%    e# Map over "11", implicitly converted to the array [0 1 ... 10]
  G+          e# Add 16. This makes sure there will be 5 binary digits: a leading 1
              e# which will have to be removed and the remaining, valid digits
    2b        e# Convert to array of binary digits
      1>      e# Remove first digit
          N*  e# Join by newlines. Implicitly converts arrays to strings

Luis Mendo

Posted 2017-02-15T22:20:11.827

Reputation: 87 464

4

PHP, 33 bytes

while($i<11)printf('%04b ',$i++);

user63956

Posted 2017-02-15T22:20:11.827

Reputation: 1 571

4

Intel 8080 machine code, MITS Altair 8800, 10 bytes

00 00 00 00 00 00 00 00 76

Listing:

0000   00   NOP      
0001   00   NOP      
0002   00   NOP      
0003   00   NOP      
0004   00   NOP      
0005   00   NOP      
0006   00   NOP      
0007   00   NOP      
0008   00   NOP      
0009   00   NOP      
000A   76   HLT 

Uses the CPU's program counter (PC, aka instruction pointer) to count from 0 to 10 then halt. The output display is conveniently in nibble format already.

Output:

Output is displayed on Blinkenlights A3-A0.

enter image description here

Try it online!

640KB

Posted 2017-02-15T22:20:11.827

Reputation: 7 149

3

MATLAB / Octave, 13 bytes

dec2bin(0:10)

Online Demo

Suever

Posted 2017-02-15T22:20:11.827

Reputation: 10 257

3

Jelly, 10, 9, 8 bytes

⁴r26BḊ€Y

Try it online!

I'm not that great at jelly, so I'd be open to any tips!

This uses Emigna's first algorithm


Thanks to Dennis for shaving off two bytes making me tie his own answer. :P

Explanation:

      Ḋ€    # Return all but the first element of each item in the list:
⁴r26        #   [16, 17, 18, ... 26]
     B      #   Converted to binary
        Y   # And joined with newlines

James

Posted 2017-02-15T22:20:11.827

Reputation: 54 537

Ḋ€ saves a byte. – Dennis – 2017-02-15T22:53:05.307

@Dennis Ah, that makes sense. Thanks! – James – 2017-02-15T22:53:51.007

⁴r27 saves another one. – Dennis – 2017-02-15T22:58:27.090

3

Python 2, 38 36 bytes

n=16;exec"print bin(n)[3:];n+=1;"*11

Thanks to @DJMcMayhem for golfing off 2 bytes!

Try it online!

Dennis

Posted 2017-02-15T22:20:11.827

Reputation: 196 637

for n in range(11):print bin(n+16)[3:] also at 38 bytes. – ETHproductions – 2017-02-15T23:17:21.130

n=16;exec"print bin(n)[3:];n+=1;"*11 is two shorter – James – 2017-02-15T23:33:55.873

@DJMcMayhem It is indeed. Thanks! :) – Dennis – 2017-02-15T23:42:48.347

2

Jelly, 8 bytes

2Ḷṗ4ḣ11Y

Try it online!

How it works

2Ḷṗ4ḣ11Y  Main link.

2Ḷ        Unlength 2; yield [0, 1].
  ṗ4      Take the fourth Cartesian power.
    ḣ11   Head 11; discard all but the first eleven elements.
       Y  Join, separating by linefeeds.

Dennis

Posted 2017-02-15T22:20:11.827

Reputation: 196 637

2

RProgN, 15 Bytes

~16.aL1{2B26q}:

This has been a very good modivation to add a pad function. The entirety of ]L4\-'0'\m\., more than half the code, is to pad.

_Saved 6 bytes thanks to @ETHProductions, that's the pad function cut in half.

Explained

~16.aL1{2B26q}:
~               # Zero Space Segment
 16.            # The literal number 16
    aL          # The length of the Alphabet
      1         # The literal number 1
       {     }: # For each number between 16 and 26 inclusive
        2B      # Convert to base 2
          26q   # Get the characters between 2 and 6 inclusive.

Try it online!

ATaco

Posted 2017-02-15T22:20:11.827

Reputation: 7 898

length of the Alphabet Nice way to save a byte ;-) – ETHproductions – 2017-02-15T23:41:31.470

2

BF, 121 101 bytes

,....>,.<...+.>.<-..+.-.>.<..+..>.<-.+.-..>.<.+.-.+.>.<-.+..-.>.<.+...>.<.-...>.<+.-..+.>.<.-.+.-.!0

Requires a trailing newline. Makes use of ! symbol (so, check the box that says !) with this interpreter (try it online!).

Potentially 51 bytes if each operator was considered as 4 bits

Timtech

Posted 2017-02-15T22:20:11.827

Reputation: 12 038

You should specify (or additionally add a byte) for the ! checkbox being enabled. – Conor O'Brien – 2017-02-16T02:32:22.670

Whoops, I'm new to that and thought it encoded it in the URL. Will specify... wait, actually, I think it's already specified in the second sentence (?), will clarify that a bit – Timtech – 2017-02-16T11:47:54.643

2

Retina, 36 33 bytes


%%%%
+`(^|\b)%
0$%'¶$%`1
11!`\d+

Try it online!

Explanation


%%%%

Replace the empty (non-existent) input with %%%%.

+`(^|\b)%
0$%'¶$%`1

On the first run of this stage, it will match ^% and essentially replace the text %%%% with the two lines 0%%% and 1%%%. The stage will loop until the output stops changing. On the second run, it will match \b% (since digits count as word characters and % doesn't), and replace the groups by duplicating them and adding 0 to one copy and 1 to the other: 0%%% becomes the lines 00%% and 01%% (and the same sort of thing for 1%%%). Through this loop all 16 bitstrings will be produced, linefeed separated.

11!`\d+

The first 11 matches of \d+ (a run of at least 1 digit) are retrieved. The matches are output in a linefeed-separated list.

Business Cat

Posted 2017-02-15T22:20:11.827

Reputation: 8 927

I'm curious in understanding how this 0$%'¶$%1line works. What do$%,`1,'¶` represent? – user41805 – 2017-02-16T06:53:29.530

@KritixiLithos Sorry I didn't explain the specifics, it's a bit convoluted :P. $%\`` represents everything before the match on the same line, and$%'is everything after the match on the same line.is a literal linefeed. So basically the replacement matches the first%on a line and replaces it with0plus the rest of the line it was on, a newline, the beginning of the line it was on, and a1`. Of course, the beginning and end of the line it was on are untouched by the replacement because they weren't part of the match. – Business Cat – 2017-02-16T14:13:42.230

So it's not putting a copy of the line after itself, but rather inserting the end of the line, a newline, and the beginning of the line in between the beginning and end of the line that remain intact. – Business Cat – 2017-02-16T14:15:25.437

Ah thanks, that was helpful :) (I'm trying to learn Retina now) – user41805 – 2017-02-16T14:30:02.380

In which case, I think you can use `G11`` as the last line of the regex instead – user41805 – 2017-02-16T15:26:26.407

2

C 170 120 bytes

n,i,m,k[4]={0};f(){for(m=0;m<=10;m++){n=m;i=0;for(;n;i++){k[i]=n;n/=2;}for(i=4;i>0;i--)printf("%d",k[i-1]%2);puts("");}}

Ungolfed version:

void f()
{
    int n,i,m,k[4]={0};


   for(m=0;m<=10;m++)
   {
      n=m;
      i=0;

      for(;n;i++)
      {
         k[i]=n;
         n/=2;
      }  
      for(i=4;i>0;i--)
         printf("%d",k[i-1]%2);

      puts("");        
   }
}

Can definitely be shortened!?

@Ahemone Awesome idea, Thanks!

Should work now! Try it online!

Abel Tom

Posted 2017-02-15T22:20:11.827

Reputation: 1 150

the first for loop in your golfed version should go to 4 rather than 3, but that doesn't matter because the loop can be eliminated entirely and the second for loop can start from 0. You can also just use while(n), but compacting the while loop down into a for loop saves more again. n/=2 will also save you a byte over the shift. You're also missing a terminating } on the golfed version causing an error on compilation. – Ahemone – 2017-02-16T12:48:18.257

@Ahemone Fixed the } and improved the code, 50 bytes shorter based on your idea. – Abel Tom – 2017-02-16T16:07:14.700

102 bytes – ceilingcat – 2018-11-07T00:30:40.750

2

Ruby, 25 bytes

11.times{|n|puts"%04b"%n}

G B

Posted 2017-02-15T22:20:11.827

Reputation: 11 099

2

C, 75 68 69 bytes

Approach 1: 75 73 74 bytes

m;p(i){putchar(i?m&i?49:48:9);}r(){for(m=11;m--;p(4),p(2),p(1),p(0))p(8);}

Try it online!


Approach 2: 68 69 bytes

m,n,o;f(){for(m=11;m--;)for(n=m,o=5;o--;n*=2)putchar(o?n&8?49:48:9);}

Try it online!

Ahemone

Posted 2017-02-15T22:20:11.827

Reputation: 608

Suggest m,n;f(o) instead of m,n,o;f() – ceilingcat – 2019-05-09T18:18:47.310

2

C#, 96 bytes


Golfed

()=>{for(int i=0;i++<11;)System.Console.WriteLine(System.Convert.ToString(i,2).PadLeft(4,'0'));}

Ungolfed

() => {
    for( int i = 0; i++ < 1; )
        System.Console.WriteLine( System.Convert.ToString( i, 2 ).PadLeft( 4, '0' ) );
}

Full code

using System;

namespace Namespace {
    class Program {
        static void Main( string[] args ) {
            m();

            Console.ReadLine();
        }

        static void m() {
            for( Int32 i = 0; i++ < 11; )
                Console.WriteLine(
                    Convert.ToString( i, 2 ). // Converts the number to binary code
                    PadLeft( 4, '0' ) );      // Fills the number with the missing '0's
        }
    }
}

Releases

  • v1.0 - 96 bytes - Initial solution.

auhmaan

Posted 2017-02-15T22:20:11.827

Reputation: 906

I like the release version you added - are you going to include RC versions as well? \o/ – Shaun Bebbers – 2017-02-16T15:20:58.927

1

Going to be honest, don't know what RC means... This is how I try to post my solutions in PPCG

– auhmaan – 2017-02-16T15:25:35.990

RC means 'Release Candidate' - i.e., you'd send out a few versions with minor differences and await to see which is the most stable by your RC number. So if you had version A and version B, you could have v1.0-RCa and v1.0-RCb or something. – Shaun Bebbers – 2017-02-16T15:29:17.603

1Oh, that. No. If I make another release, I increment the Version Number right away. – auhmaan – 2017-02-16T15:33:36.030

2

R - 23

We can use intToBin function from the R.utils package:

R.utils::intToBin(0:10)

[1] "0000" "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" "1001" "1010"

bouncyball

Posted 2017-02-15T22:20:11.827

Reputation: 401

2

C, 68 bytes

f(){puts("0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010");}

Somehow, this is the shortest C answer so far…

EDIT: I was missing 1000 somehow. Well, it’s still winning.

Lynn

Posted 2017-02-15T22:20:11.827

Reputation: 55 648

Isn't this missing 8? (1000) – fəˈnɛtɪk – 2017-03-13T17:07:38.390

1

Python 2, 44 bytes

for x in range(11):print bin(x)[2:].zfill(4)

This uses the zfill function which works like rjust except it always padds with 0 so you don't waste bytes on an argument.

Post Rock Garf Hunter

Posted 2017-02-15T22:20:11.827

Reputation: 55 382

Wait what, this whole time I've been wasting bytes making my own padding function? (lambda k,l:' '*(len(k)-l)+k) Wow... +1 just because of this :D – HyperNeutrino – 2017-03-01T03:29:55.493

1

Pyke, 8 bytes

TFw0+b2t

Try it here!

TFw0+b2t - for i in range(10):
  w0+    -    i+16
     b2  -   bin(^)
       t -  ^[:-1]

Also 8 bytes:

TF 4@b2t

Try it here!

   4@    - set_bit(4, i)

Blue

Posted 2017-02-15T22:20:11.827

Reputation: 26 661

1

Ruby, 38 bytes

11.times{|i|puts i.to_s(2).rjust 4,?0}

dkudriavtsev

Posted 2017-02-15T22:20:11.827

Reputation: 5 781

-1 byte by getting rid of the parentheses: 11.times{|i|puts i.to_s(2).rjust 4,?0} – Conor O'Brien – 2017-02-16T02:33:55.183

1

Pyth - 8 7 bytes

<5^_`T4

Try it online here.

Maltysen

Posted 2017-02-15T22:20:11.827

Reputation: 25 023

1

stacked, 30 bytes

11:>[2 baserep'0'4 pad out]map

Try it online!

11:> is a range from 0 to 10. The rest is rather self-explanatory.

Other solutions that I've found:

11:>[bits 4 dpad''join out]map
11:>[bits 4 dpad$tostrmap]map out
11~>15+[bits behead''join out]map
16 26|>[bits behead''join out]map

Conor O'Brien

Posted 2017-02-15T22:20:11.827

Reputation: 36 228

1

BF, 134 bytes

I'm sure this can be shortened--it's pretty much my first BF golf.

++++++++++[>+>+>+++++>>>+++++>>>+++++>>>+++++[<<<]>>>-]>>+>[-->>+>]<<<[<<<]>>[>[>>-[<<+.->]<[>]>-[<<.>]<[>]>++>]<-[<<<-]++<<[<<<]>.>-]

Try it online! Assumes a tape infinite in both directions, like the interpreter at TIO uses. An interpreter where < at the left end of the tape is a no-op would save three bytes.

Explanation

More than half of the code (the first 77 bytes, to be precise) is spent initializing the tape. The steps go like this:

++++++++++
10|

[>+>+>+++++>>>+++++>>>+++++>>>+++++[<<<]>>>-]
 0|10|10|50| 0| 0|50| 0| 0|50| 0| 0|50|

>>+>[-->>+>]<<<[<<<]>>
 0|10|11|48| 0| 1|48| 0| 1|48| 0| 1|48| 0| 1|

The cells initialized to 1 store the bits of our number plus 1: 1 represents a zero bit and 2 represents a one bit.

The initialization phase ended with the pointer on the 11. Now we use this cell to run 11 iterations of our loop:

[>          Move to the first 48
 [>>-       While we're still on a 48, move 2 cells over and decrement
  [         The cell's value now equals the bit it represents; if it's not 0:
   <<+.-    Move to the 48, increment, output, and decrement again
   >        Move to the next cell, which holds a 0
  ]         Leave the loop
  <[>]>     Pointer shenanigans to get back on the cell representing the bit
  -         Decrement again: cell is 255 for a zero bit, 0 for a one bit
  [         If cell is not 0:
   <<.>     Move to the 48, output, and move to the 0 cell
  ]
  <[>]>++   Get back on the bit's cell; increment back to original value
  >         Move to the next 48
 ]          Loop exits once we've output all four bits
            Now we increment the binary number: a one bit turns into a zero bit and
            carries; a zero bit turns into a one bit and doesn't carry
 <-         Move back to the rightmost bit cell and decrement
 [          If it is not 0, it must represent a one
  <<<-      Leave it decremented, go to the next bit cell and decrement it too
 ]          Loop exits on a bit cell that represented a zero
 ++         Increment it twice (to represent a one)
 <<[<<<]    Move back to first cell on tape
 >.         Move to 10 cell and output (newline)
 >-         Move to loop counter cell and decrement
]

DLosc

Posted 2017-02-15T22:20:11.827

Reputation: 21 213

1

C, 110? bytes

n,i,m,k[4]={0};f(){for(m=-1;(n=++m)<11;i=0){for(;n;n>>=1)k[i++]=n;for(i=4;i--;)printf("%d",k[i]&1);puts("");}}

Ungolfed:

int f() {
    int n,i,m,k[4]={0};
    for(m=-1;(n=++m)<11;i=0) {
        for(;n;n>>=1)
            k[i++]=n;
        for(i=4;i--;)
            printf("%d",k[i]&1);
        puts("");
    }
}

ice cube tray

Posted 2017-02-15T22:20:11.827

Reputation: 11

1

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.

coredump

Posted 2017-02-15T22:20:11.827

Reputation: 6 292

1Nice solution! You can save 2 bytes with: (format t"0000,~49,'0,,4:b"78187493530) - instead of space use comma as separator – None – 2017-02-24T18:00:33.147

1Could you explain how does this directive work? ~mincol,padchar, and what later? I know 4 is length of group and :b for binary system. Are there more arguments later? Does it work similiar to ~R? – None – 2017-02-24T18:18:36.160

1@PrzemysławP I added some explanations. It is the same for ~R, but with five parameters (the first one is the radix, i.e. the base). – coredump – 2017-02-24T18:43:03.387

1(format t"~53,,,4:b"8800979740570) saves additional 5 bytes - uses fact that any order is allowed. – None – 2017-02-24T21:53:11.897

1@PrzemysławP That's clever, I didn't pay attention to the order. Btw, I also removed "53" because in fact the size is not necessary. Thanks a lot. – coredump – 2017-02-25T06:55:22.160

1

SAS, 39 bytes

Not very creative...

data t;do i=0 to 10;put i binary4.;end;

J_Lard

Posted 2017-02-15T22:20:11.827

Reputation: 351

1

Ruby, 44 bytes

(0..10).each{|e|puts (16+e).to_s(2)[-4..-1]}

jose_castro_arnaud

Posted 2017-02-15T22:20:11.827

Reputation: 229

1

Common Lisp, 41 bytes

(dotimes(i 11)(format t"~5,,,'0<~B ~>"i))

Different method than other CL submission.

user65167

Posted 2017-02-15T22:20:11.827

Reputation:

1

Perl 6, 25 22 bytes

printf "%04b "x 11,^11

Fairly similar to the ruby answer. EDIT: Saved 3 bytes by removing parantheses.

Håvard Nygård

Posted 2017-02-15T22:20:11.827

Reputation: 341

1

Java 8, 195 bytes

interface C{static void main(String[]a){for(int i=0;i++<11;)System.out.println(((java.util.function.Function<String,String>)x->("0000"+x).substring(x.length())).apply(Integer.toString(i-1,2)));}}

I am never golfing in Java again...

-2 bytes thanks to ceilingcat

HyperNeutrino

Posted 2017-02-15T22:20:11.827

Reputation: 26 575

@ceilingcat appears you are right, thanks! – HyperNeutrino – 2019-11-13T15:31:25.073

1

REXX, 32 bytes

do i=0 to 10
  say x2b(d2x(i))
  end

idrougge

Posted 2017-02-15T22:20:11.827

Reputation: 641

1

Commodore VIC-20/VC-20/C64 - 95 69 68 67 tokenized BASIC bytes used (obfuscated and minimized)

0dEfnb(x)=sgn(xandb):fOi=0to10:b=8:fOj=0to3:?rI(stR(fnb(i)),1);:b=b/2:nE:?:nE

Copy and paste the above text into WinVice, for instance. Just about fits onto 80 characters on a Commodore 64.

Here's the non-obfuscated symbolic listing for illustrative purposes:

0 DEF FN B(X)=SGN(X AND B)
1 FOR I=0 TO 10
2  LET B=8
3  FOR J=0 TO 3
4   PRINT RIGHT$(STR$(FN B(I)),1);
5   LET B=B/2
6  NEXT J
7  PRINT
8 NEXT I

Of course this may be minimised and further obfuscated. Explanation:

The DEF FN command in line zero is a simple function which accepts one numeric parameter. This will give the algebraic sign the number passed to it as a parameter, in this case is ANDed with the value B (declared elsewhere, but becomes global once declared).

Line 1 starts the loop counter I

Line 2 sets the value of the 3rd BIT.

Line 3 starts loop counter J (we want bits 0 - 3 essentially).

Line 4 gets the STR$ value (string value) of the function FN B(I), this is then passed to the RIGHT$ command as CBM BASIC auto-pads outputted numbers with a white space, so we get the last character in the string created by STR$.

Line 5 moves the BIT counter down one (like x >> 1 in C I suppose).

Line 6 moves the J loop up one.

Line 7 prints a new line (line echo PHP_EOL; in PHP).

Line 8 moves the I loop up one.

I've 'nested' the listing in the example to better show the loop and loop order.

C64 counting in nybbles

Shaun Bebbers

Posted 2017-02-15T22:20:11.827

Reputation: 1 814

Thanks, I've done this in the obfuscated version above (although not the NEXT J one) - I'll give it a go for that as well, saving another BASIC byte! – Shaun Bebbers – 2019-02-07T11:06:33.757

1

Japt, 6 bytes

Aò¤ù'0

Test it

Aò¤ù'0
A          :10
 ò         :Range [0,A]
  ¤        :Convert each to binary
   ù       :left pad each to length of longest
    '0     :  With "0"

Shaggy

Posted 2017-02-15T22:20:11.827

Reputation: 24 623

1

Haskell, 92 bytes

0#_="";b#n=(if even$n`div`b then"0"else"1")++(div b 2#n)
main=putStr.unwords.map(8#)$[0..10]

Try it online!

Jonathan Frech

Posted 2017-02-15T22:20:11.827

Reputation: 6 681

1

Java 8, 77 bytes

()->{for(int i=43;i>=0;i--)System.out.print(((i/4>>i%4)&1)+(i%4==0?" ":""));}

For each number, print one bit at a time by bit shifting then masking with &1.

jkenney

Posted 2017-02-15T22:20:11.827

Reputation: 21

1

Perl 5, 22 bytes

printf'%04b 'x11,0..10

Try it online!

Xcali

Posted 2017-02-15T22:20:11.827

Reputation: 7 671

1

Rust, 37 bytes

||for i in 0..11{print!("{:04b} ",i)}

Try it online!

Herman L

Posted 2017-02-15T22:20:11.827

Reputation: 3 611

1

Forth (gforth), 57 bytes

: f 11. do 4. do j 3 i - rshift 2 mod 1 .r loop cr loop ;

Try it online!

Explanation

  • Loop from 0 to 10 (inclusive)
    • For each iteration:
    • Loop from 0 to 3, print the character at that index (with no space)
    • Print a newline

Code Explanation

: f                 \ start a new word definition
  11.               \ place 11 and 0 on stack
  do                \ loop from 0 to 10
    4.              \ place 4 and 0 on the stack
    do              \ loop from 0 to 3
      j             \ place outer loop index (current number) on stack
      3 i -         \ get the position of the digit we care about
      rshift 2 mod  \ get the value at that position
      1 .r          \ print the binary digit with no space appended
    loop            \ end the inner loop
    cr              \ print a newline
  loop              \ end the outer loop
;                   \ end the word definition

reffu

Posted 2017-02-15T22:20:11.827

Reputation: 1 361

1

MathGolf, 9 bytes

Ar☻+mÅà╞n

Try it online.

Explanation:

Ar        # Push a list in the range [0,11): [0,1,2,3,4,5,6,7,8,9,10]
  ☻+      # Add 16 to each: [16,17,18,19,20,21,22,23,24,25,26]
    m     # Map each value to,
     Å    # using the following two commands:
      à   #  Convert the value to a binary string
       ╞  #  And remove the first digit
    n     # After the map: join the list by newlines
          # (after which the entire stack joined together is output implicitly)

The first four bytes could alternatively be A╒E+:

A╒        # Push a list in the range [1,11]: [1,2,3,4,5,6,7,8,9,10,11]
  E+      # Add 15 to each: [16,17,18,19,20,21,22,23,24,25,26]

Kevin Cruijssen

Posted 2017-02-15T22:20:11.827

Reputation: 67 575

1

x86-16, IBM PC DOS, 28 bytes

Binary (build NIB.COM with xxd -r):

00000000: b30a 53b1 04d2 e3b8 000e d0d3 1430 cd10  ..S..........0..
00000010: e2f5 b820 0ecd 105b 4b79 e7c3            ... ...[Ky..

Unassembled listing:

B3 0A       MOV  BL, 10         ; set initial counter 
        NIB_LOOP: 
53          PUSH BX             ; save counter in BX 
B1 04       MOV  CL, 4          ; shift / loop 4 times 
D2 E3       SHL  BL, CL         ; shift left one nibble/nybble 
        BIT_LOOP:    
B8 0E00     MOV  AX, 0E00H      ; BIOS write to console / zero AL
D0 D3       RCL  BL, 1          ; rotate left one bit, MSB into carry 
14 30       ADC  AL, '0'        ; add carry bit to '0' ASCII char 
CD 10       INT  10H            ; write to console 
E2 F5       LOOP BIT_LOOP       ; keep looping to next bit 
B8 0E20     MOV  AX, 0E20H      ; BIOS write to console / space 
CD 10       INT  10H            ; write to console 
5B          POP  BX             ; restore counter 
4B          DEC  BX             ; is >= 0? 
79 E7       JNS  NIB_LOOP       ; if so, keep looping 
C3          RET                 ; exit to DOS 

Output

enter image description here

Standalone PC DOS executable .COM program. Output is to console, descending separated by spaces.

640KB

Posted 2017-02-15T22:20:11.827

Reputation: 7 149

1

Wren, 107 106 bytes

for(i in[0,1])for(j in[0,1])for(k in[0,1])[0,1].each{|l|
System.printAll([i,j,k,l])
if(i==1&&k==1)Fn.x()
}

Try it online!

Explanation

for(i in[0,1])for(j in[0,1])for(k in[0,1])[0,1].each{|l| // Define all those binary digits
System.printAll([i,j,k,l])                                // Print all the binary digits
if(i==1&&k==1)                                            // If the number has reached 10,
              Fn.x()                                      // Execute an undefined command in the Fn class
                                                          // That breaks out of all of the 4 for loops and stops the program
}

user85052

Posted 2017-02-15T22:20:11.827

Reputation: