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 0
s followed by a two 1
s, 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 -> ~<~<<<<
positive integers
\$\endgroup\$:
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
\$\endgroup\$