I reverse the source code, you negate the input!

Blatant rip-off of a rip-off. Go upvote those!

Your task, if you wish to accept it, is to write a program/function that outputs/returns its integer input/argument. The tricky part is that if I reverse your source code, the output must be the original integer negated.

Examples

Let's say your source code is ABC and its input is 4. If I write CBA instead and run it, the output must be -4.

Let's say your source code is ABC and its input is -2. If I write CBA instead and run it, the output must be 2.

An input of 0 may give 0 or -0, however, if you do support signed zero, -0 should give 0.

• Why do we need a copy of the same question? – Christian Sep 18 '19 at 7:30
• @Christian That one outputs a constant number (and its negation) whereas this one has to take input and return/negate it. A very different job in a lot of languages. – Adám Sep 18 '19 at 7:32
• A yes, now I see the difference. One needs to read VERY carefully – Christian Sep 18 '19 at 7:34
• If using a structured language like C#, are you just reversing lines? – Taco タコス Sep 19 '19 at 2:31
• @PerpetualJ No, look at the source like list of characters, some of which are line breaks. – Adám Sep 19 '19 at 5:42

J, 3 bytes

-&0


Try it online!

-&0 is "argument minus 0"

0&- is "0 minus argument"

• this is really quite nice :) – Conor O'Brien Sep 18 '19 at 2:59
• Clever, I didn't think of that one, but rather ][- etc. – Adám Sep 18 '19 at 5:35

PowerShell, 18 14 bytes

$args#"sgra$"-


First of the trivial comment-abuse answers

• Oh, God. Not this again. – S.S. Anne Sep 17 '19 at 19:57
• "First of the trivial comment-abuse answers" Redeemed by the TIO links! – Jonah Sep 18 '19 at 22:06

JavaScript, 11 bytes

n=>n//n->=n


x86 machine code, 3 bytes

C3 D8 F7


The above bytes of code define a function that is a no-op: it simply returns control to the caller. That function is followed by two garbage bytes that will not be executed, since they come after a return—they are in "no man's land". In assembler mnemonics:

ret                     ; C3
fdiv  st(0), st(7)      ; D8 F7


Okay, so now some troll comes by and reverses the order of the bytes:

F7 D8 C3


These bytes now define a function that takes an integer argument in the EAX register, negates it, and returns control to the caller. In assembler mnemonics:

neg  eax     ; F7 D8
ret          ; C3


So…that was simple. :-)

Note that we can make the "negation" instruction be anything we want, since it is never executed in the "forward" orientation and only executed in the "reversed" orientation. Therefore, we can follow the same pattern to do arbitrarily more complicated stuff. For example, here we take an integer argument in a different register (say, EDI, to follow the System V calling convention commonly used on *nix systems), negate it, and return it in the conventional EAX register:

C3      ret
D8 F7   fdiv  st(0), st(7)      ;  \ garbage bytes that
F8      clc                     ;  | never get executed,
89      .byte 0x89              ;  / so nobody cares


↓ ↓

89 F8   mov  eax, edi
F7 D8   neg  eax
C3      ret


Jelly, 2 bytes

oN

oN - (input) OR ((input) negated)

No - ((input) negated) OR (input)

• Heh, you can also do ḷN, no need for the OR logic. :D – Erik the Outgolfer Sep 17 '19 at 21:54
• Na, words give aN aesthetic effect :) – Jonathan Allan Sep 17 '19 at 22:05

Haskell, 8 bytes

Anonymous identity function, turning into subtraction from 0 when reversed.

id--)-0(


Try it online!

Reversed:

(0-)--di


Try it online!

• You’ve managed to break the code highlighting with your reversed version! – Tim Sep 19 '19 at 9:57
• @Tim Curious. Testing suggests it fails when a comment starts right after a right parenthesis. – Ørjan Johansen Sep 20 '19 at 2:07

Whitespace, 48 bytes

S S S N
S N
S T N
T   T   T   T   T   T   N
S T N
N
N
T   S N
T   N
S S T   N
T   T   S S T   T   T   T   T   N
T   S N
S N
S S S


Letters S (space), T (tab), and N (new-line) added as highlighting only.

Minor modification of my Whitespace answer for the I reverse the source code, you negate the output! challenge.

Try it online or try it online reversed (with raw spaces, tabs and new-lines only).

Explanation:

Utilizing the Exit Program builtin being a short palindrome NNN.
The regular program will:

SSSN   # Push 0 to the stack
SNS    # Duplicate it
TNTT   # Read STDIN as integer, and store it at heap address 0
TTT    # Retrieve the input from heap address 0, and push it to the stack
TNST   # Pop and print the top of the stack as number
NNN    # Exit the program, making everything after it no-ops


The reverse program will:

SSSN   # Push 0 to the stack
SNS    # Duplicate it
TNTT   # Read STDIN as integer, and store it at heap address 0
TTT    # Retrieve the input from heap address 0, and push it to the stack
SSTTN  # Push -1 to the stack
TSSN   # Multiply the top two values on the stack together
TNST   # Pop and print the top of the stack as number
NNN    # Exit the program, making everything after it no-ops


Small additional explanation of pushing a number:

• First S: Enable Stack Manipulation
• Second S: Push a number to the stack
• S or T: Positive/negative respectively
• Some S/T followed by a trailing N: number in binary, where S=0 and T=1

I.e. SSTTSTSN pushes -10. For the 0 we don't need an explicit S=0, so simply SSSN or SSTN is enough.

R, 23 bytes

I decided to give it a go without the comment trick.

Forward

+=scan;""+-0;nacs=+


Try it online!

Reverse

+=scan;0-+"";nacs=+


Try it online!

In the forward version + is acting a binary operator, and - is a unary operator.

In the reverse the + becomes unary and the - is binary. So scan function takes the arguments: file="" which means stdin and what=0, which are also defaults. So when the + is unary the first argument is on the right, when it is binary the first argument is on the left.

The

;nacs=+


part of the code does nothing really useful, so in a sense my code is not really very much more valid than using the comment trick.

• This is very smart (+1). We often redefine the R operators to golf bytes, but I think this is the first time I have seen + redefined to be used as both unary and binary. It took me a minute to understand how this was parsed… No other operator name would have done the job. – Robin Ryder Sep 19 '19 at 15:45

C (clang), 23 bytes

f(*x){}//};x*-=x*{)x*(g


Try it online!

Labyrinth / Hexagony, 6 bytes

Labyrinth:

?!@!?


Hexagony:

?!@!~?


How?

?       - take a signed integer
( / ~) - negate
!       - output top-of-stack / current-memory-edge
@       - exit


Perl 6 / Raku, 3 bytes

*-0


Try it online!

Creates a Whatever code block. Read in normally its standard block equivalent is -> \x {x - 0}, but in reverse it becomes -> \x {0 - x}.

Python 3, 22 bytes

lambda x:x#x-:x adbmal


Try it online!

A lambda which implements the identity function (or negation)

MarioLANG, 22 bytes

;:
=#
:)!
--
<(
"
[>
;


Try it online!

He just inputs and outputs the number before he falls to EOF

reversed:

;
>[
"
(<
--
!):
#=
:;


Try it online!

He loops until the input value is 0 and the output value is -input, the he says the number.

Brain-Flak, 7 bytes

#)]}{[(


Try it online!

Reversed:

([{}])#


Try it online!

Note: Only works in interpreters that support comments (e.g. works in Rain-Flak, but not in BrainHack)

If we also swap opening/closing brackets instead of just reversing the bytes we can do this in 8 bytes without using comments:

({}[{}])

• Is this undefined behaviour abuse? I don't think Brain-Flak specification allows such parenthesis. – null Sep 18 '19 at 14:06
• @TwilightSparkle The # starts a comment, so the parenthesis in the original version are ignored. – Riley Sep 18 '19 at 14:08
• Oh yeah, I forgot! But it only works in Rain-Flak then(It's the official intepreter though). You will probably need to mention it? – null Sep 18 '19 at 14:12
• @TwilightSparkle added a note for clarification. Thanks. – Riley Sep 18 '19 at 14:21
• Fun little challenge: Can you do this without comments if you also swap opening/closing brackets instead of just reversing? – DJMcMayhem Sep 18 '19 at 21:42

Haskell, 12 bytes

f=id;x-0=x f


Try it online! Reverse:

f x=0-x;di=f


Try it online!

Not as short as Ørjan Johansen's answer, but without comments.

Perl 5 (-p), 7 6 bytes

-1 thanks to @primo

$_*=$#


TIO

A comment doesn't change input

#1-=*_$ Negate the input $_*=-1#


TIO

• -1: $_*=$# TIO. Note that the # must be the very last byte of the program, otherwise it will be interpreted as the variable $#, rather than the last index of the array with name <empty>. – primo Sep 18 '19 at 16:32 • however i don't understand how it works because trying to print $# gives either an error (if # is not the last character) or nothing – Nahuel Fouilleul Sep 18 '19 at 18:08
• Seems to only work with -p or -n. I suspect the boilerplate has something to do with it... – primo Sep 19 '19 at 11:26
• @primo It works because -p/-n adds a ; after the code. Which means that $# is actually $#;: the size of the array @;. If the size of @; changes, the result isn't correct anymore (TIO). Anyway, this is super clever, well done! :) – Dada Sep 20 '19 at 15:31
• that's the explanation could be seen with perl -MO=Deparse -p <(echo -n '$_*=$#'), because it seems perl -MO=Deparse -pe '$_*=$#' adds a newline – Nahuel Fouilleul Sep 21 '19 at 6:18

Gaia, 2 bytes

_@


Try it online!

_	| implicit push input and negate
@	| push next input OR push last input (when all inputs have been pushed)
| implicit print TOS


Reversed:

@	| push input
_	| negate
| implicit print TOS


Backhand, 6 5 bytes

I@-Ov


Made a little complex due to the nature of the pointer in Backhand. I don't think it's possible to get any shorter haha, turns out I was wrong. This duplicates no instruction and reuses both the input, output and terminate commands between the two programs. Now I think it is optimal, since you need all of the IO-@ commands to work, and in a 4 byte program you can only execute two of those commands.

Explanation:

The pointer in Backhand moves at three cells a tick and bounces off the boundaries of the cell, which means the general logic is overlapping. However you can manipulate this speed with the v and ^ commands.

The original program executes the instructions IO-@, which is input as number, output as number, subtract, terminate. Obviously the subtract is superfluous. In the code these are:

I@-Ov
^  ^    Reflect
^     Reflect again
^


The reversed program executes v-I-vO-@. The v reduces the pointer steps between ticks, and the - subtracts from the bottom of the stack, which is implicitly zero. The extra - commands do nothing. The program executes like

vO-@I
v       Reduce pointer speed to 2
-     Subtract zero from zero
I   Get input as number and reflect off boundary
-     Subtract input from zero
v       Reduce pointer speed to 1
O      Output as number
-     Subtract zero from zero
@    Terminate



R, 14 bytes

scan()#)(nacs-


Try it online!

A full program that reads a number, or reads and negates a number. The reverse functionality is protected by an inline comment

• Your brackets are the wrong way around in the commented part. – Aaron Hayman Sep 18 '19 at 22:17
• @AaronHayman thanks! – Nick Kennedy Sep 18 '19 at 22:29

Python 3, 22 14 bytes

int#__bus__. 0


Try it online!

Uses the int class's constructor and a built-in pseudo-private method.

• Huh. Why is the space before the attribute mandatory? – Jo King Sep 19 '19 at 1:25
• @JoKing 0. would be interpreted as a number, which is followed by a symbol – att Sep 19 '19 at 8:22

Wolfram Language (Mathematica), 9 bytes

1&0+#-0&1


Try it online!

Forward: read ((1)&*0+#-0)&*1=#&

Backward: read ((1)&*0-#+0)&*1=-#&

APL (Dyalog Unicode), 13 3 bytes

-∘0


Try it online!

Trivial answer. Returns arg or ¯arg.

Saved 10 bytes by not being dumb (thanks Adám).

Altered the resulting 3-byter to a more fitting function.

• Whoa, this can be done trivially in three bytes! – Adám Sep 17 '19 at 20:31
• Interestingly, you already have a 3-byte answer embedded as a substring of this. – Adám Sep 18 '19 at 9:01
• @Adám yeah, I knew there was a simple answer in there somewhere. Thanks. – J. Sallé Sep 18 '19 at 12:54

Batch, 34 bytes

@ECHO.%1 2>MER@
@REM>2 1%=-aa/TES@


Echoes (ECHO.) the input (%1). The rest of the first line technically redirects STDERR to a file called MER@, but this isn't impactful.
Second line is commented out (REM...).

Reversed

@SET/aa-=%1 2>MER@
@REM>2 1%.OHCE@


Uses the arithmetic mode of the set command (SET /a) to subtract (-=) the input (%1) from an undefined variable (a) which is equivalent to 0 - input. Again, the rest of the first line technically redirects STDERR to a file called MER@, but this isn't impactful.
Second line is commented out (REM...).

• This looks interesting. Care to explain? – Adám Sep 25 '19 at 5:34
• @Adám Added an explanation, and also realised that I had the programs around backwards. – Οurous Sep 25 '19 at 6:34

Excel VBA, 12

?[A1]']1A[-?


Reversed:

?-[A1]']1A[?


Input is cell A1 of the ActiveSheet. Comments still work in the Immediate Window :)

• I get what you are going for here, but I am pretty sure that you meant to answer ?[A1]']1A[-? – Taylor Scott Jan 3 '20 at 12:53
• @TaylorScott A good catch, oops – Chronocidal Jan 3 '20 at 15:52

Vyxal, 2 bytes

#N


Try it Online!

!enilnO ti yrT

#N is a comment, so nothing happens and the input is outputted unchanged.

N# negates the input, which is then outputted implicitly.

If you’re not a fan of using comments for something like this, the # can be replaced with any of ? , ₴ ⁰, etc.

05AB1E, 2 bytes

(I


Try it online!

Reversed

(    negates nothing
I  pushes input

I    pushes input
(  negates input

• It is supposed to only negate the input in one direction, leaving it as-is in the other. Clearly, 1-character solution cannot be valid. – Adám Sep 17 '19 at 19:49
• My bad, I misunderstood – jasanborn Sep 17 '19 at 19:52
• Your answer has a couple mistakes. First of all, the explanation for (I is slightly flawed: it doesn't negate nothing, it negates the (implicit) input. But pushing the input with I means the output is the input instead. Your other problem is that both links you put link to I(. – Makonede Apr 1 at 23:19

Befunge-98 (FBBI), 6 bytes

&.@.-&


Klein, 2 bytes

Works in all 12 topologies!

@-


Try it online!

Reverse

-@


Try it online!

- negates the input and @ ends the program

Turing Machine Language, 39 bytes

The Positive

1 r - _ 0
0 l * * 0
0 - _ l 0
0 _ _ r 0


The Negative

0 r _ _ 0
0 l _ - 0
0 * * l 0
0 _ - r 1


This one was a bit trickier than I thought, mostly because I had to get past my prejudices of having code that runs with 'compile' errors.

><>, 5 4 bytes

n-r0


uses stack initialisation with the -v option, put your input variable there.

Try it online!

Or try the reversal

Explanation

n       Prints whatever is on the stack as a number
-      Subtract the top 2 elements on the stack.
There aren't 2 elements, so it crashes.
r0    Never gets executed

or reversed:

0       Push a 0 onto the stack
r      reverse the stack (now 0, -v)
-     Subtract top 2 elements and push result (0-v, ie negated)
n    Print as number
The code wraps around and executes again.
It crashes on the - as there is only one
item on the stack: 0.