Polyglot code to print "FREE " 21 times in both (obfuscated) C and brainf*ck. The C version prints an additional newline character at the end.
/****************************************************************************/
/****[****/
#define x 0x0a
#define o putchar
#define q *p
#define w <<1
char a
[x]={0}
;char q
=a;void
main(){q+=x;++
p;++q;++p;++p;
++q;++ q; ++p;++ q;++q; p-=04;
while (q){++p;q+= (*(p+-1)w) %0x6;++p;
q+=(* (p-2 )w)% (x w) ;q-= (*(p- 2)w)%
0x6;++ p;q +=((* (p+-3 )w)%x) w;++p;
q+=((( *(p -4)w)%x)+((* (p-4)w)%0x6))>>
1;p-=( x-2 )>>1; --q;
}++p; while (q){ ++p; o(q) ;++p
;o(q) ;-- p;--q;o(q); o(q);++q;++p; ++p
;o(q) ;p-= 0xf>>q% x;--q;}o(x );}
/*]++++++++++>+>>++>++<<<<[>++>+++++++>++++++++>+++<<<<-]>[>.>.<-..+>>.<<<-]*/
Both codes follow the same general procedure.
An array of memory cells holds integer values to control the program flow or be interpreted as ASCII characters. Five cells are used:
Cell | Purpose |
---|---|
0 | Loop counter |
1 | Loop counter |
2 | Character |
3 | Character |
4 | Character |
The first loop counter controls a loop to increment values in the other cells. The second loop counter controls the number of times "FREE " is printed.
All cells initially hold a value of 0.
At the beginning of execution, the first loop counter is set to 10, the second loop counter is set to 1, and the character cells 3 and 4 are set to 2. (Cell 2 is not modified and remains set to 0.)
++++++++++
>+
>>++
>++
The instruction pointer is moved back to cell 0 (the first loop counter) and the first loop is started. The first loop iterates 10 times. During each iteration it adds 2 to cell 1, 7 to cell 2, 8 to cell 3, and 3 to cell 4, then subtracts 1 from cell 0. (The C version follows a different procedure with the same results at the end of the loop.)
<<<<[
>++
>+++++++
>++++++++
>+++
<<<<-
]
At the end of the first loop, the cell values are as follows:
Cell | Value |
---|---|
0 | 0 |
1 | 21 |
2 | 70 (ASCII "F") |
3 | 82 (ASCII "R") |
4 | 32 (ASCII " ") |
Next, the instruction pointer is moved to cell 1 and the second loop is started. The second loop iterates 21 times. During each iteration, it prints cell 2, then cell 3, subtracts 1 from cell 2, prints cell 2 twice, adds 1 to cell 2, prints cell 4, and subtracts 1 from cell 1.
>[
>.
>.
<-..+
>>.
<<<-
]