# Stay away from zero

Given a non-negative integer n, output 1 if n is 0, and output the value of n otherwise.

# Input

A non-negative integer.

# Testcases

input output
0      1
1      1
2      2
3      3
4      4
5      5
6      6
7      7


# Scoring

This is , so shortest answer in bytes wins.

Standard loopholes apply.

• You should probably put a link to the TNB CMC, since that's where this challenge came from. – mbomb007 May 3 '17 at 20:53
• Does the answer need to be a full function, or can it be the body? – Caleb Kleveter May 3 '17 at 20:58
• @CalebKleveter The default rule in PPCG is that the answer is either a function or a full program, but not snippets. – Leaky Nun May 4 '17 at 1:56
• Can we print the output with a leading zero? – MD XF Dec 26 '17 at 21:54
• @MDXF yes, you can. – Leaky Nun Dec 26 '17 at 22:30

## Mathematica, 9 8 bytes

Per Martin Ender:

#~Max~1&


First idea:

#/. 0->1&


Pure function with replaces 0 with 1. The space is necessary or it thinks we are dividing by .0.

$_||=1  Takes input on separate lines from stdin. Runs with the flags -lp. # Taxi, 517 bytes Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l 2 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Knots Landing.Go to Knots Landing:n 2 r 2 r 1 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 1 l 2 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:n 1 r 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.  Formatted for humans: Go to Post Office:w 1 l 1 r 1 l. Pickup a passenger going to The Babelfishery. Go to The Babelfishery:s 1 l 1 r. Pickup a passenger going to Cyclone. Go to Cyclone:n 1 l 1 l 2 r. Pickup a passenger going to Addition Alley. Pickup a passenger going to Knots Landing. Go to Knots Landing:n 2 r 2 r 1 l. Pickup a passenger going to Addition Alley. Go to Addition Alley:w 1 r 1 l 2 l. Pickup a passenger going to The Babelfishery. Go to The Babelfishery:n 1 r 1 r. Pickup a passenger going to Post Office. Go to Post Office:n 1 l 1 r.  The key location is Knots Landing which performs the NOT operation so 0 returns 1 and all other numbers return 0. Now, you can go to Addition Alley to add the result from Knots Landing to the original input. # x86-64 Assembly, 10 9 bytes Following the standard System V AMD64 calling convention, this function accepts a 32-bit unsigned integer parameter via the EDI register, and returns the result via the EAX register: 89 F8 | mov eax, edi ; move parameter from EDI to EAX 85 C0 | test eax, eax ; test input and set flags 75 01 | jnz Finished ; if input is non-zero, then jump to end FF 40 | inc eax ; otherwise, input is 0, so increment it to 1 | Finished: C3 | ret ; return, leaving result in EAX  # x86-32 Assembly, 8 bytes We could also write this as 32-bit code, since we're only dealing with 32-bit values. The nice thing about this is that the INC instruction becomes only 1 byte in length. (In 32-bit mode, inc eax can be encoded simply as 40. In 64-bit mode, 40 is interpreted as a REX prefix, so the leading FF is needed. See Intel's documentation for this instruction.) The caveat is that most 32-bit calling conventions pass parameters on the stack, rather than in registers, and loading a value from the stack (memory) takes many more bytes. If we can be allowed a __fastcall-style calling convention that passes parameters in registers (supported by virtually all C compilers, so not really cheating, just a bit less standard), then the integer parameter is passed in ECX and we get the following code, for a total of 8 bytes: 89 C8 | mov eax, ecx 85 C0 | test eax, eax 75 01 | jnz Finished 40 | inc eax | Finished: C3 | ret  • Welcome to PPCG! – Justin May 5 '17 at 15:35 • xchg eax, edi is only 1 byte and is fine since you don't need to keep a copy of the value. Or use a calling convention like GCC's regparm (-mregparm=3) that passes the first arg in EAX already. Tips for golfing in x86/x64 machine code – Peter Cordes Jan 28 at 3:52 • Or following an ARM machine-code answer's idea, mov eax, edi / neg edi (setting CF for any non-zero input) / sbb eax, -1 to add 1 or 0 (-(CF + -1)). Unfortunately no shorter unless we can use sbb al, -1 (2 bytes) – Peter Cordes Jan 28 at 3:56 • Thanks, @Peter, for the review. I wrote this answer back before I knew it was legitimate to adopt custom calling conventions in assembly answers. I probably won't update it myself at this point; feel free to do so without stepping on my toes. Or post a separate answer. – Cody Gray Jan 28 at 4:11 • There's already a quite clever 5-byte x86 answer with the function entry point in the middle of a block, using jecxz to run an inc ecx, taking full advantage of fastcall and of 1-byte xchg – Peter Cordes Jan 28 at 4:17 # Arnold C, 303 bytes IT'S SHOWTIME HEY CHRISTMAS TREE i YOU SET US UP 1 GET YOUR ASS TO MARS i DO IT NOW I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY BECAUSE I'M GOING TO SAY PLEASE i TALK TO THE HAND i BULLSHIT TALK TO THE HAND 1 YOU HAVE NO RESPECT FOR LOGIC YOU HAVE BEEN TERMINATED  Trying to explain it: IT'S SHOWTIME //main() HEY CHRISTMAS TREE i //int i YOU SET US UP 1 //i = 1 GET YOUR ASS TO MARS i // ? compiler told me to add that DO IT NOW // ? compiler told me to add that I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY // something related to reading from stdin BECAUSE I'M GOING TO SAY PLEASE i // if(i) TALK TO THE HAND i //print i BULLSHIT //else TALK TO THE HAND 1 //print 1 YOU HAVE NO RESPECT FOR LOGIC //endif YOU HAVE BEEN TERMINATED //end main()  It even beats this answer! # x86 opcode, 6 5 bytes  41 INC ECX entry: E3 FD JECXZ SHORT$-1
91                         XCHG    EAX,ECX
C3                         RETN


ECX -> EAX

• You'll need to add a length in bytes, so other users can tell how long it is. Generally assembly or byte/op-code languages are measured in the size of the instructions the source represents. – Οurous Dec 18 '17 at 21:15
• fixed. forgot.   – l4m2 Dec 18 '17 at 21:26
• Damn, I just came up with E3 01 49 41 C3. – user99151 Jan 28 at 4:48
• @2x-1 yeah if returning in ECX it can be 1B shorter – l4m2 Feb 4 at 9:11

# GCC command line, 9 bytes

-Df(x)x?:


Try it online!

-D in GCC checks if the following content has an =; if not, it add the code

#define <followed_content> 1


which in this case turns into

#define f(x)x?: 1

• On meta I gave a common solution but here I give a pointed one – l4m2 Dec 20 '17 at 18:20
• Can the downvoter leave a comment? I find this very creative. – MD XF Dec 26 '17 at 21:36

# Java 8, 9 bytes

n->n|1>>n


Try it online.

# C# .NET, 9 bytes

n=>n|1>>n


Try it online.

Different approach (and 1 byte shorter) than the existing Java/C# .NET answers. (Also works with negative inputs.)

Explanation:

i->       // Method with integer as both parameter and return-type
i|      //  Return the input bit-wise OR-ed with:
1>>i  //  1 bit-wise right-shifted by the input


1>>i will be 1 when the input is 0, and 0 for every other input.
0|1 for input 0 will therefore result in 1, and n|0 for every other input will therefore result in n.

• See? You're getting better at bitwise operations ;-) – Olivier Grégoire Apr 13 '18 at 9:30

# Sinclair ZX81/Timex TS1000/1500 BASIC ~19 tokenized BASIC bytes

 1 INPUT A
2 PRINT A+NOT A


This is a more efficient solution than the one below; essentially it takes the NOT value of A (which would be zero in all cases but zero) and adds that to the value of A.

### Sinclair ZX81/Timex TS1000/1500 BASIC, ~31 tokenized BASIC bytes

 1 INPUT A
2 IF NOT A THEN LET A=NOT A
3 PRINT A


This takes a numeric input from the user and will display 1 if zero is entered by making A NOT A; otherwise it does nothing and displays the numeric value.

# Binary Lambda Calculus, 2.125 Bytes

00010110 00110001 0


Translation into lambda calculus:

\n. n (\x. n) (\x. x)


# Labyrinth, 6 bytes

?"1
@!


Try it online!

?   Take number input and push it
"   No-op, but a 3-way junction:
If top is 0, go straight
1     Convert the 0 to 1 and bounce off the dead end
"     Enter the junction again, turn right (reflected to south)
!@    Pop and print as number, turn around the corner and halt

Otherwise, top is positive; turn right
!@    Pop and print as number and halt


<?=$argn?:1;  Online Version # Jelly, 2 bytes o1  Try it online! An alternative to the other Jelly solution. o provides a default value for a zero/empty argument; in this case, we default it to 1. • And another alternative is »1 – Jonathan Allan May 2 '17 at 17:39 • Now you're making me wonder if it's possible in 1 byte; I doubt it, but you have to wonder. The closest I've got is ḃ, which outputs the number itself for all nonzero numbers, and nothing if the input is 0. – user62131 May 2 '17 at 17:46 • Yeah, another close one is m which yields [0,0] for 0 and n for n. – Jonathan Allan May 2 '17 at 17:52 # Binary-Encoded Golfical, 12 bytes This binary encoding can be converted back to the standard graphical representation using the encoder provided in the Golfical github repo, or run directly using the interpreter by adding the -x flag. Hexdump of binary encoding: 00 40 02 15 17 14 00 01 23 1D 17 14  Original image: Magnified 120x, with color labels: # Perl 5, 10 +1 byte for -p flag = 11 bytes $_=$_*1||1  Run with -p flag. • $_=$_||1 and thus $_||=1 is sufficient – ikegami May 3 '17 at 16:51
• Also, the other answers don't include the costs of outputting the result, so no need to count -p. – ikegami May 3 '17 at 16:52

# Brain-Flak, 14 bytes

((){{}[()]}{})


Try it online!

Just computes: 1 + (n ? n-1 : 0).

• – James May 3 '17 at 21:08

# Whirl, 38 bytes

01100011100011110011111100001000111100


Try it online!

Explanation:

01100     op.ccw, op.intio    Mem1 = STDIN
011100    math.ccw, math.=    If (Mem1 = 0) Then (Math.Val = 1) Else (Math.Val = 0)
0111100   op.cw, op.one       Op.Val=1
11111100  math.add            Math.Val = Math.Val + Mem1
00        op.one              Op.Val=1 (Cheapest way to loop back to the Math wheel)
100       math.store          Mem1 = Math.Val
0111100   op.ccw, op.intio    STDOUT = Mem1


## Ohm, 11 6 bytes

ö?┼¿1;


Uses CP-437 character encoding. Run with -c flag

Explanation:

ö?┼¿1;
■print(                                            )
┼    ■      first_input()
?   ; ■                   if(                     )
ö      ■                      int(first_input())!=0
¿   ■                                            else
1  ■                                                 1

• Here's a list of things you could fix: (1) if you just say this is in CP437 (no need for the -c flag), you only need to count characters. (2) Ohm is written in Ruby, so 0 is a truthy value. However, there is a built-in x != 0 component (ö). (3) There is implicit printing, so , is not necessary. (4) Ohm is on TIO now, so it'd help if you added a link to it. – Nick Clifford May 2 '17 at 19:57
• FYI, feel free to ping me in chat if you have any questions about/feature reqs for the language! – Nick Clifford May 2 '17 at 20:11
• Again, there's no need for the -c flag. That's just for reading files; it's not necessary for PPCG submissions. Just say it's in CP437 and you're good. – Nick Clifford May 2 '17 at 20:36

# k, 2 bytes

1|


Finds the maximum of 1 and whatever number is given.

Try it online!