18
4
Wise is a simple bitwise language I designed a while back. It is based around Python's bitwise operations. It has several operations most of these are the same or very similar to the equivalent symbol in Python.
:Duplicate the top of the stack?Rotate the top of the stack to the bottom!Rotate the bottom of the stack to the top[]loop while the top of the stack is not zero~not the top of the stack (-(n+1))-negate the top of the stack (-n)>bitshift the top of the stack once to the right (n//2)<bitshift the top of the stack once to the left (n*2)^xor the top two items of the stack (Same as Python)|or the top two items of the stack (Same as Python)&and the top two items of the stack (Same as Python)
Making an integer in Wise is pretty simple you can make zero with ::^ and increment it with ~- so you make zero and increment it a bunch of times. However if we remove the - things become a little more interesting.
We can still make every number using the remaining operations. For example here is 3
~<<~
This works because ~ turns zero, an infinite string of 0 bits, into negative one, an infinite string of 1 bits, each < appends a 0 bit to the end, when we are done we do ~ which turns each it into a string of 0s followed by a two 1s, or as most people call it 3.
Task
Write a program that when given a positive integer will output a Wise program that will create the number n without any - in its source (the source of the output, you may use - in your own source). You may assume that there is already a zero on the top of the stack.
This is code-golf not meta-golf so you should aim to minimize the generating source code not necessarily the output.
Example outputs
This list is not exhaustive they are simply possible outputs
1 -> ~<~
2 -> ~<~<
3 -> ~<<~
4 -> ~<~<<
5 -> ~<~:<<|
6 -> ~<<~<
7 -> ~<<<~
8 -> ~<~<<<
9 -> ~<~:<<<|
10 -> ~<~:<<|<
11 -> ~<<~:><<<|
12 -> ~<<~<<
13 -> ~<<~:<<<|>
14 -> ~<<<~<
15 -> ~<<<<~
16 -> ~<~<<<<
is 0 included in
positive integers– colsw – 2017-05-06T18:57:26.3874No, 0 is not included in positive integers. – Zacharý – 2017-05-06T18:58:38.350
Apparently
:applied on an empty stack pushes a0. I think this should be specified, as it's not obvious that duplicating from an empty stack should give0– Luis Mendo – 2017-05-06T22:32:26.130Are other characters syntax errors, or are they ignored? – xnor – 2017-05-06T23:07:50.797
@Luismendo you do not know the contents of the stack other than that that too if the stack is a zero – Post Rock Garf Hunter – 2017-05-07T00:27:43.220
@xnor they are ignored – Post Rock Garf Hunter – 2017-05-07T00:28:04.363