# Bitflip and negate

Given an integer, make an expression that produces it from 0 using unary negation - and bitwise complement ~ (~n = -n-1), with the operators applied right to left.

...
-3  = ~-~-~0
-2  = ~-~0
-1  = ~0
0  = 0
1  = -~0
2  = -~-~0
3  = -~-~-~0
...

Your expression must be as short as possible, which means no redundant parts of ~~, --, -0, or 00. Output or print the expression as a string or a sequence of characters.

• So... you want us to put our thing down, flip it and reverse it? Sep 7 '16 at 20:42
• whitespace between ~ and 0 allowed?
Sep 7 '16 at 20:54
• No, output the strings exactly.
– xnor
Sep 7 '16 at 21:54
• Obligatory xkcd.com/153 Sep 8 '16 at 20:17

# Python, 32 bytes

lambda x:("-~"*abs(x))[x<0:]+"0"

Anonymous lambda function. Given an integer x writes "-~" abs(x) times and removes the first char if x is negative, then a zero is added to the end.

• Aw, beat me to it. Sep 7 '16 at 19:43
• I just wrote the same too - with n in the place of x and ' in the place of " :) Sep 7 '16 at 23:30
• @JonathanAllan Then you can safely consider it a dupe. Sep 8 '16 at 12:03

# JavaScript (ES6), 33 31 bytes

f=x=>x<0?"~"+f(~x):x&&"-"+f(-x)

Recursion < built-ins < loops (at least in this case). Basically unevaluates the input:

• if it's less than 0, flip it and add a ~ to the string;
• if it's more than 0, negate it and add a - to the string;
• if it's exactly 0, return 0.

0         = 0
-1 = ~( 0) = ~0
+1 = -(-1) = -~0
-2 = ~(+1) = ~-~0
+2 = -(-2) = -~-~0
-3 = ~(+2) = ~-~-~0
+3 = -(-3) = -~-~-~0
etc.

# Pyth, 1413 12 Bytes

_<>0Q+0sm"~-

-2 Bytes thanks to @StevenH.

test suite

Decided to try out Pyth, so i translated my python answer to it. Any help welcome!

### Explanation:

_<>0Q+0sm"~-
m"~-     # Map "~-" onto the input (= a list of n times "~-").
s         # Join the list to a string.
+0          # Add "0" in front.
<>0Q            # Slice off the last char if the input is negative.
_                # Reverse the whole thing.
• Use implicit input at the end to save one byte: >0 instead of <Q0 Sep 7 '16 at 20:21
• @StevenH. Thank you! Now we're in a tie with the shortest answer! Sep 7 '16 at 20:31
• Very different solution (that, unfortunately, doesn't save any bytes): tW>0Q_+0sm"~- Sep 7 '16 at 20:45
• @StevenH. Golfed your solution down to 12: _<>0Q+0sm"~- I hope your okay with me adding this to my solution. Sep 7 '16 at 22:44

# C, 46 bytes

m(x){putchar(x?x<0?126:45:48);x&&m(-x-(x<0));}

Unlike most (all?) other answers, this one outputs the operators ~ and - one by one.

# 05AB1E, 14 13 bytes

Ä„-~×¹0‹i¦}0J

Explanation

„-~           # the string "-~"
Ä   ×          # repeated abs(input) times
¹0‹i¦}    # if input is negative, remove the first char
0J  # join with 0

Try it online!

# Retina, 19 17 bytes

Replace the number with unary, with a zero on the end. Replace each 1 with -~. Remove double negative if there is one.

\d+

• You probably meant -p instead of -r. Also you can get rid of those last parenthesis and semicolon : if$h<0 is enough. – Dada Sep 7 '16 at 21:03 • I did, thanks. I've been writing too many answers in sed I guess. Sep 7 '16 at 21:05 • Probably, yea. (Get rid of the last 2 parenthesis too) – Dada Sep 7 '16 at 21:09 • You can also save 2 bytes by doing$h<0&&s;.; instead of s/.// if $h<0. (-p adds a ; at the end of the code, so no need for the last ; of s;.;;. And a if b is roughly equivalent to b && a, but in this case it saves you one byte because you can remove the space) – Dada Sep 7 '16 at 21:19 • Thanks, I didn't know -p added a ; too. Sep 7 '16 at 21:22 # Dyalog APL, 18 bytes '0',⍨0∘>↓'-~'⍴⍨2×| '0',⍨ character zero appended to 0∘> negativeness (i.e. 1 for numbers under 0; 0 for zero and up) dropped from '-~'⍴⍨ the string "~-" cyclically reshaped to length two times | the absolute value + plus 0∘< positiveness (i.e 1 for numbers over 0) TryAPL online! # Haskell, 41 bytes f n=['-'|n>0]++(tail$[1..abs n]>>"-~")++"0"

f n|n<0=tail$f(-n)|x<-[1..n]>>"-~"=x++"0" Thanks to nimi for 3 bytes • tail fails for n=0. You can use drop 1 instead. – nimi Sep 7 '16 at 20:25 • @nimi Thanks; I have no idea how I missed that.. Sep 7 '16 at 20:31 • Don't waste the otherwise guard: f n|n<0=tail.f$abs n|x<-[1..n]>>"-~"=x++"0".
– nimi
Sep 7 '16 at 20:42

# Explanation:

{
substr
# string repeat ｢-~｣ by the absolute value of the input
'-~' x .abs

# concatenate 0 to that
~ 0

,

# ignore the first character of the string if it is negative
0 > $_ } # Jelly, 14 12 bytes -2 bytes thanks to @Dennis (return 0 rather than concatenate "0", making this a full program only.) 0>‘ A⁾-~ẋṫÇ0 Test it at TryItOnline How? 0>‘ - link 1 takes an argument, the input 0> - greater than 0? 1 if true 0 if false ‘ - increment A⁾-~ẋṫÇ0 - main link takes an argument, the input Ç - y = result of previous link as a monad A - x = absolute value of input ⁾-~ - the string "-~" ẋ - repeat the sting x times ṫ - tail repeatedString[y:] (y will be 1 or 2, Jelly lists are 1-based) 0 - implicit print then return 0 ## ><>, 18 + 3 = 22 bytes :?!n0$-:0):1go-
-~

Try it online! +3 bytes for the ​ -v flag to initialise the stack with the input. If assuming that STDIN is empty is okay, then the following is a byte shorter:

:?!ni*:0):1go-
-~

The program keeps flipping the input n as necessary until it reaches 0, after which it errors out.

[Loop]
:?!n      If n is 0, output it as a num. If this happens then the stack is now
empty, and the next subtraction fails

# PHP, 58 bytes

<?=((0<$a=$argv[1])?'-':'').str_pad('0',2*abs(\$a),'~-',0);

## Labyrinth, 25 bytes

?+#~.
.  ; 6
54_"#2
@!

Try it online!

### Explanation

I really like the control flow in this one. The IP runs in a figure 8 (or actually a ∞, I guess) through the code to reduce the input slowly to 0 while printing the corresponding characters.

The code starts in the upper left corner going right. The  doesn't do anything right now. ? reads the input and + adds it to the implicit zero below. Of course that doesn't do anything either, but when we run over this code again, ? will push a zero (because we're at EOF), and + will then get rid of that zero.

Next the # pushes the stack depth, simply to ensure that there's a positive value on the stack to make the IP turn south, and ; discards it again.

The " is a no-op and acts as the main branch of the code. There are three cases to distinguish:

• If the current value is positive, the IP turns right (west) and completes one round of the left loop:

_45.?+
_45      Push 45.
.     Print as character '-'.
Negate the current value (thereby applying the unary minus).
?+  Does nothing.

• If the current value is negative, the IP turns left (east) and the following code is run:

#26.~
#        Push stack depth, 1.
26      Turn it into a 126.
.     Print as character '~'.
~    Bitwise NOT of the current value (applying the ~).

Note that these two will alternate (since both change the sign of the input) until the input value is reduced to zero. At that point...

• When the current value is zero, the IP simply keeps moving south, and executes the ! and then turns west onto the @. ! prints the 0 and @ terminates the program.

# GolfScript, 3024 20 bytes

• Saved 6 bytes thanks to xnor.
• Saved 4 bytes thanks to Dennis.

~."-~"\abs*\0<{(;}*0

Input: -5

Output: -5 = ~-~-~-~-~0

### Explanation

~.     # Input to integer and duplicate
"-~"   # We shall output a repetition of this string
\abs   # Move the input onto the stack and computes abs
*      # Multiply "-~" for abs(input) times
\      # Copy onto the stack the input
0<     # Is it less than 0?
{(;}*  # Yes: remove first '-' from the output
0      # Push 0

Try it online!

• You don't have to print the 2 = , just the -~-~0.
– xnor
Sep 11 '16 at 7:21
• You can use {(;}*0 instead of {(;}{}if 0. Sep 11 '16 at 7:47