18
2
Write a non-empty program or function that when called outputs a single value, 1 or 0, and when called multiple times, the output numbers produce the binary representation of your program's source code (in the same code page from which your code is compiled/interpreted).
For instance, if your source code was abc
(in ASCII), the outputs would be:
1st call: 0 // ASCII letter 'a'
2nd call: 1
3rd call: 1
4th call: 0
5th call: 0
6th call: 0
7th call: 0
8th call: 1
9th call: 0 // ASCII letter 'b'
10th call: 1
11th call: 1
12th call: 0
13th call: 0
14th call: 0
15th call: 1
16th call: 0
17th call: 0 // ASCII letter 'c'
18th call: 1
19th call: 1
20th call: 0
21st call: 0
22nd call: 0
23rd call: 1
24th call: 1
After the 24th call, the behaviour is undefined.
The binary representation of the source must contain at least one 0 bit and one 1 bit.
Instead of 1 and 0, you can output any two distinct, consistent values (such as true
and false
).
Self-modifying programs that output the binary representation of the original source are allowed, provided that they don't read the source code to find out what to print next.
This is code-golf, so the shortest answer in bytes wins.
Is this a polyglot with JavaScript? – Stan Strum – 2018-02-23T19:43:09.443