19
\$\begingroup\$

This is tangentially related to my quest to invent an esoteric programming language.

A table of the binary numbers 0 .. 15 can be used to implement a Universal Binary Function using indexing operations. Given two 1-bit inputs X and Y, all 16 possible functions can be encoded in a 4-bit opcode.

X Y  F|0 1 2 3 4 5 6 7 8 9 A B C D E F
- -    - - - - - - - - - - - - - - - -  
0 0    0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 1    0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
1 0    0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
1 1    0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
       -     -   - -   -   -   -   - -
       0    ~X  ~Y ^   &   Y   X   | 1
      ZERO    NOT-Y   AND         OR
          NOT-X   XOR              ONE

So this set of 16 functions can be applied to binary inputs as the function

U(f,x,y): (f >> ((x<<1) | y)) & 1,

or

U(f,x,y): (f / 2^(x×2 + y)) % 2,

or with indexing or matrix partitioning.

It will be useful to know the most compact way to represent or generate such a table of values for any possible languages to be built upon this type of binary operation.

The goal:

Generate this exact text output:

0101010101010101
0011001100110011
0000111100001111
0000000011111111

That's it! Shortest-code wins.

\$\endgroup\$
  • 2
    \$\begingroup\$ I had an intuition that the APL-family would do well here. :) \$\endgroup\$ – luser droog Jul 25 '13 at 9:09
  • \$\begingroup\$ Also related: A simple logic gate calculator \$\endgroup\$ – FireFly Nov 7 '14 at 13:39
  • \$\begingroup\$ Are leading or trailing newlines accepted? \$\endgroup\$ – Titus Sep 20 '16 at 17:10
  • \$\begingroup\$ Yes, extra newlines are fine. \$\endgroup\$ – luser droog Sep 20 '16 at 17:32

33 Answers 33

1
\$\begingroup\$

C 70 bytes

c,k=1,i;x(){putchar(48+i);i^=!(++c%k);c%16||(k*=2)+puts("");k>8||x();}

use recursive function... in https://ideone.com/ it compile and result:

0101010101010101
0011001100110011
0000111100001111
0000000011111111

even if in the compiler i use it does not compile...

\$\endgroup\$
  • \$\begingroup\$ all functions that use global variables, if they use their value 0, can be ok only one time or only if global variable are at start 0 \$\endgroup\$ – RosLuP Sep 20 '16 at 21:00
1
\$\begingroup\$

PHP , 56 Bytes

for(;$i<64;$i++)echo($i%16?"":"\n").($i&2**($i>>4)?1:0);

86 Bytes

for($i=1;$i<9;$i*=2)echo str_repeat(str_pad(decbin(2**$i-1),2*$i,0,0),16/(2*$i))."\n";
\$\endgroup\$
1
\$\begingroup\$

PHP, 76 70 68 67 61 57 45 bytes

Now it´s worth it: code shorter than its output. :)

for(;$x<64;$x++)echo$x%16?"":"
",$x>>$x/16&1;

run with -r.

runs through rows and columns in one loop:
$x/16 rounded is the row number, $x%16 the column and decimal value of the binary value we want to display:

In columns 0,16,32,48 ($x%%16 falsy) print a linebreak.
If bit $x/16 is set ($x>>$x/16&1), print 1, else 0.


add 2 bytes for trailing instead of leading linebreak:

for(;$x<64;$x++)echo$x>>$x/16&1,$x+1&15?"":"
";
\$\endgroup\$

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.