30
\$\begingroup\$

Your task is to write a program, function, or snippet that will output one value if run forward, and another if run backward. The program must run without errors.

The output can be any two distinct values, of the same data type. For example, 1 and 3, true and false, or 't' and 'f'. In languages without a concept of data types, such as brainf*** or text, any distinct values are allowed.

This is a code golf challenge, shortest answer per language wins. Note that snippets are allowed (so something like 1-2 is valid, but don't expect any upvotes).

\$\endgroup\$
11
  • 3
    \$\begingroup\$ Is it just me, or does this seem like it would be an interesting candidate for code-bowling? \$\endgroup\$
    – Xcali
    Commented Nov 10, 2019 at 6:50
  • 2
    \$\begingroup\$ Does backwards mean character by character or line by line? \$\endgroup\$ Commented Nov 11, 2019 at 17:16
  • 1
    \$\begingroup\$ @CaptainMan Character by character \$\endgroup\$ Commented Nov 11, 2019 at 20:07
  • 3
    \$\begingroup\$ As much rep as someone wants if said person can think of a 1 byte answer. \$\endgroup\$
    – Makonede
    Commented Feb 4, 2021 at 19:06
  • 1
    \$\begingroup\$ Can the output be nondeterministic? (But they will always still be different) \$\endgroup\$
    – pxeger
    Commented Jun 27, 2022 at 6:30

58 Answers 58

1
2
2
\$\begingroup\$

Befunge-98 (PyFunge), 2 bytes

@,

Try it online!

Prints nothing by default, prints a null byte if reversed

\$\endgroup\$
2
\$\begingroup\$

Turing Machine Code, no comments or string literals, 19 bytes

0 * 0 * 0
0 * 1 * 0

Reversed:

0 * 1 * 0
0 * 0 * 0

Try it online!

The code runs without errors, but does loop forever. Prints '0' forward, prints '1' reversed.

\$\endgroup\$
2
\$\begingroup\$

Deadfish~, 2 bytes

oi

Try it online!

Boy, if that fruit hanged any lower, it'd be underground I tell you.

\$\endgroup\$
2
\$\begingroup\$

TI-BASIC Non-trivial, 4 5 bytes

Disp "Disp :

This outputs 'Disp :'

Since TI-BASIC is token based, reversing it gives

:Disp "Disp

which outputs 'Disp'

Let me explain. 'Disp ' is one token which prints the given string to the home screen. First we are giving it a string of 'Disp :', so simple there. When reversed, since each 'Disp ' is one byte, we get the reversed code above. This reversed code starts with a ':', which ends the line, and means nothing here. The remaining code is 'Disp "Disp ', which runs and outputs 'Disp ' to the home screen.

TI-BASIC Trivial, 2 bytes

If a program ends with a values, then it outputs it. This lets us do

12

which outputs 12, and reversed is

21

which outputs 21.

\$\endgroup\$
1
  • \$\begingroup\$ I was waiting for a TI-Basic answer, +1 \$\endgroup\$ Commented Nov 11, 2019 at 20:11
2
\$\begingroup\$

Bitwise Cyclic Tag, 2 bits (0.25 bytes)

With an input of 1 this should either set the data string to a null string or a string of length 2 in its next iteration.

01

Explanation:

0  Delete the leftmost data bit.
   Halt. No more instructions executed.
 1 Not executed

Reversed:

1  If the left-most data-bit is 1,
 0 Append a 0 to the data-string.
\$\endgroup\$
2
\$\begingroup\$

Java, 14 bytes

()->1;//;0>-)(

Simple function that uses comments to exclude the other part. Outputs 1, and reversed outputs 0.

\$\endgroup\$
2
\$\begingroup\$

AppleSoft Basic, 3 bytes

0?1 outputs 1
1?0 outputs 0

0?"?1 <=> 1?"?0

verified here

\$\endgroup\$
2
\$\begingroup\$

Halfwit, 1 byte

*k

Try It Online!

The second valid one-byte solution :P

* is multiplication, multiplying two 0s. k is a digraph character, and on its own a NOP, but k* is exponentiation.

Alternatively, anything that's a single char before > works as > delimits a compressed integer, so >x will push some number, and x> will apply x then push 0.

\$\endgroup\$
2
\$\begingroup\$

HBL, 1 byte

+2

Run forwards, outputs 3:

+   Increment
 2  Two

Run backwards, outputs 8:

2   Double
 +  Four

In fact, any program consisting of two characters from the set 2<+-/% will work. Here are all the possible combinations together with their outputs. We could also add 0 to the set if it weren't for the "same data type" restriction.

\$\endgroup\$
2
\$\begingroup\$

Ly, 2 bytes

-`

Try it online!

Forwards outputs 1 and backwards shows -1.

Why? Forwards:

 -   : subtract top two stack entries, when empty produces "0" 
  `  : increment top of stack, produces "1"

Backwards:

`   : increment top of stack, when empty produces "0"
 -  : subtract top two stack entries, produces "-1"
\$\endgroup\$
1
  • \$\begingroup\$ ok... will do. I thought multiple entries from the same person were discouraged. My bad... \$\endgroup\$
    – cnamejj
    Commented Jun 27, 2022 at 18:49
2
\$\begingroup\$

Vyxal, 3 bytes

2-1

Try it Online!

Running it forward gives 1, running it backwards gives 2.

This because vyxal's syntax is weird:

ab- (vyxal) = a-b (py)
\$\endgroup\$
1
  • \$\begingroup\$ This can be 10 for 2 bytes \$\endgroup\$
    – lyxal
    Commented Oct 18, 2022 at 13:22
2
\$\begingroup\$

Brachylog, 2 bytes

+1

Try it online!

There are probably loads of different 2 byte answers.

Explanation

+1 states that 1 is the result of the sum of an unknown list of integers, which is obviously true. (there are infinitely such lists).

1+ states that we can sum the list 1 into some result, which is obviously false. since 1 isn’t a list.

\$\endgroup\$
1
\$\begingroup\$

Japt, 2 bytes

c1

Return x rounded up to nearest multiple of N => output 0

Test it

1c

x default to 1 => output 1

Test it

\$\endgroup\$
1
\$\begingroup\$

PHP, 2 bytes

12

Try it online!

Prints 12 ...

21

Try it online!

Prints 21 ...


Looking for non-trivial solution?

PHP, 11 bytes

<?=1;#;2=?<

Try it online!

Prints 1 ...

<?=2;#;1=?<

Try it online!

Prints 2 ...


Don't like comment abuse?

PHP, 13 bytes

;echo+1-ohce;

Try it online!

Prints 1 ...

;echo-1+ohce;

Try it online!

Prints -1 ...

Note: this one is an exact copy of my answer here: https://codegolf.stackexchange.com/a/193169

\$\endgroup\$
3
  • \$\begingroup\$ The definition of "reversed" is ambiguous in the question, and could be byte-reversed, as you have, line-reversed... or bit-reversed, which means you can get it down to one byte (eg J is binary 0100 1010, which reversed is 01010 0010, or R). I don't think you can get shorter than that, though, as I don't think sub-byte sizes are valid input to the parser. \$\endgroup\$ Commented Nov 9, 2019 at 15:41
  • \$\begingroup\$ @DewiMorgan Reversed in terms of characters \$\endgroup\$ Commented Nov 10, 2019 at 15:16
  • \$\begingroup\$ @RedwolfPrograms I get that it's what you meant, but since it was left ambiguous, it makes that aspect of the request fair game for golfing, too. All three could give interesting results, though I think the bitwise reverse would be the most trivial in many languages, and impossible in others, and the winner would likely be any language which allowed sub-byte inputs; while the command-wise reversal would for once give no advantage to golfing languages, if the number of operators is being counted, rather than number of characters. \$\endgroup\$ Commented Nov 11, 2019 at 17:47
1
\$\begingroup\$

C (gcc), 21 bytes

puts("");//;)"0"(stup

Try it online!

!enilno ti yrT

\$\endgroup\$
1
\$\begingroup\$

Runic Enchantments, 3 bytes

0@1

Try it online!

Prints 0 forwards and 1 backwards.

If no output is allowed, then @1 saves a byte.

\$\endgroup\$
2
  • \$\begingroup\$ Never realized you made this language...seems pretty cool! \$\endgroup\$ Commented Nov 10, 2019 at 1:49
  • 1
    \$\begingroup\$ @RedwolfPrograms Yep! Its almost the only language Igolf in because of that. \$\endgroup\$ Commented Nov 10, 2019 at 16:56
1
\$\begingroup\$

Rebol, 2 bytes

#1

Prints #1

1#

Prints ##

\$\endgroup\$
1
\$\begingroup\$

Whitespace, 20 bytes

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

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

Outputs 0/-1.

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

Explanation:

Very minor modification of my answer here (outputting 0 is 1 byte shorter than 1), so I'm just gonna copy-paste the explanation:

Utilizing the Exit Program builtin being a short palindrome NNN (also note that without an exit, every Whitespace program would result in an error, making it invalid for this challenge).
The regular program will:

SSSN   # Push 0 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:

SSTTN  # Push -1 to the stack
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. Only the first three and trailing N are mandatory, so SSSN/SSTN will both push 0.

\$\endgroup\$
1
\$\begingroup\$

SimpleTemplate, 10 bytes

This was actually hard to optimize, without going into the trivial answer.

{@echo"1"}

Outputs the string "1".

When reversing, you get the following:

}"1"ohce@{

Outputs the string }"1"ohce@{.

As stated in the question: "The output can be any two distinct values, of the same data type.", and both solutions output a distinct string.

You can test this on http://sandbox.onlinephpfunctions.com/code/ec4bf4b3efa93c63c6c5c9f1458330da0178b6cd
(On line 960, change between the $code and $reversed variables)



SimpleTemplate, 2 bytes

This one is extremelly trivial:

<any 2 different bytes>

Reversing them will output whatever is reversed.

\$\endgroup\$
1
\$\begingroup\$

Ahead, 4 bytes

O@j1

Prints 0 forward and 1 backward.

Forward

O     pop stack and print number
 @    end
  j1  not reached

Backward

1     push 1
 j    jump next cell
   O  pop stack and print number
  @   end

Control flow is more interesting backward because I tried to reduce the amount of redundant characters.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

GolfScript, 2 bytes

Simply prints 1 because the duplicate instruction duplicates the null string.

.1

Reversed

Outputs 11 because that is the value 1 duplicated.

1.

Try it online!

GolfScript, 4 bytes

Outputs 0 because 1 - 1 is 0.

1 1-

Reversed

Outputs -11 due to concatenation.

-1 1

Try it online!

GolfScript, 2 bytes (Am I allowed to use the null string?)

Outputs the null string, because the null string from the input minus 1 is still the null string.

1-

Reversed:

Pushes -1 onto the stack.

-1

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Null string and -1 are same data type in GolfScript? \$\endgroup\$ Commented May 8, 2023 at 18:42
1
\$\begingroup\$

@, 2 bytes

Outputs \n1, where \n represents a literal newline.

\1

Reversed

Returns 1, the expression after 1 is ignored.

1\

Try it online!

\$\endgroup\$
1
\$\begingroup\$

33, 4 bytes

o1ao

Try it online!

Outputs 01 if run forwards or 00 if run backward

\$\endgroup\$
1
\$\begingroup\$

Actually, 2 bytes

12

Try it online!

Prints 2\n1\n forwards, and 1\n2\n backwards.

\$\endgroup\$
1
\$\begingroup\$

Dis, 2 bytes

{!

Try on dis.web! Outputs one NULL character.

!bew.sid no yrT Outputs nothing.

\$\endgroup\$
1
\$\begingroup\$

><> (Fish), 3 bytes

0n1

Try it forwards

Try it backwards 1n0

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 4 bytes

?12?

prints 12\n\n forwards and 21\n\n backwards

\$\endgroup\$
0
\$\begingroup\$

Triangular, 3 bytes

i.%

Try it online!

Prints 1.

%.i prints 0.

If it weren't invalid to do an integer and a character, %# would've been a nice 2-byter.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.