# Counting in binary nybbles

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).

• Yes, spaces, commas, a comma and then a space or a "\r\n" equivalent in your chosen language. Feb 15 '17 at 22:29
• No sorry as that looks like 4 individual zero digits and not a 4-bit wide binary number. Feb 15 '17 at 22:33
• 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? Feb 15 '17 at 23:12
• They're nibbles, not nybbles. Feb 16 '17 at 22:08
• Not according to the Commodore 64 Programmers reference guide Feb 16 '17 at 22:13

# 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



# 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!

• Wait... the Cartesian product of the digits of a number? That's just... Feb 15 '17 at 22:47

## 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?

# 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.

• Nice one. I got stuck at Aô_¤ Feb 16 '17 at 1:06

# Bash + GNU utils, 26

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

• seq -w 0 1010 should work. Feb 16 '17 at 1:07
• @Dennis Thanks - I don't remember using the -w option to seq before. Feb 16 '17 at 1:23

# 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


# J, 6 bytes

#:i.11


Thanks to miles for cutting it down to 6 bytes!

• #:i.11 should work just as well Feb 24 '17 at 1:58
• I'm not sure this is valid, as per the answer to a now deleted comment.
Mar 1 '17 at 5:57
• @Adám I can't view it. Could you please explain why it's not vaild? Mar 1 '17 at 7:20
• 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.
Mar 1 '17 at 7:22

# 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.

# 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!

• i=11 (new line) while i:i-=1;print(f"{i:04b}"), for 35. Feb 16 '17 at 1:44

# PHP, 33 bytes

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


# 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


# 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.

Try it online!

# MATLAB / Octave, 13 bytes

dec2bin(0:10)


Online Demo

# 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

• Ḋ€ saves a byte. Feb 15 '17 at 22:53
• @Dennis Ah, that makes sense. Thanks! Feb 15 '17 at 22:53
• ⁴r27 saves another one. Feb 15 '17 at 22:58

# 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!

• for n in range(11):print bin(n+16)[3:] also at 38 bytes. Feb 15 '17 at 23:17
• n=16;exec"print bin(n)[3:];n+=1;"*11 is two shorter Feb 15 '17 at 23:33
• @DJMcMayhem It is indeed. Thanks! :) Feb 15 '17 at 23:42

# SmileBASIC, 26 bytes

FOR I=0TO&HA?BIN$(I,4)NEXT  # 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.  # 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! • length of the Alphabet Nice way to save a byte ;-) Feb 15 '17 at 23:41 # 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. • I'm curious in understanding how this 0$%'¶$%1 line works. What do $%, 1, '¶ represent? Feb 16 '17 at 6:53
• @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 with 0 plus the rest of the line it was on, a newline, the beginning of the line it was on, and a 1. 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. Feb 16 '17 at 14:13
• 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. Feb 16 '17 at 14:15
• Ah thanks, that was helpful :) (I'm trying to learn Retina now) Feb 16 '17 at 14:30
• In which case, I think you can use G11 as the last line of the regex instead Feb 16 '17 at 15:26

## Ruby, 25 bytes

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


# 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

• You should specify (or additionally add a byte) for the ! checkbox being enabled. Feb 16 '17 at 2:32
• 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 Feb 16 '17 at 11:47

## 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();

}

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.
• I like the release version you added - are you going to include RC versions as well? \o/ Feb 16 '17 at 15:20
• Going to be honest, don't know what RC means... This is how I try to post my solutions in PPCG Feb 16 '17 at 15:25
• 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. Feb 16 '17 at 15:29
• Oh, that. No. If I make another release, I increment the Version Number right away. Feb 16 '17 at 15:33

## 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!

• 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. Feb 16 '17 at 12:48
• @Ahemone Fixed the } and improved the code, 50 bytes shorter based on your idea. Feb 16 '17 at 16:07
• 102 bytes Nov 7 '18 at 0:30

## 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"


# C, 7568 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!

• Suggest m,n;f(o) instead of m,n,o;f() May 9 '19 at 18:18

# 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.

• Isn't this missing 8? (1000) Mar 13 '17 at 17:07

# 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.

• 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 Mar 1 '17 at 3:29

## 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)


# Pyth - 8 7 bytes

<5^_T4


# 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
`