Swap the parity

Given a positive integer n, output n+1 if n is odd, and output n-1 if n is even.

Input

A positive integer. You may assume that the integer is within the handling capability of the language.

Output

A positive integer, specified above.

Testcases

input output
1      2
2      1
3      4
4      3
5      6
6      5
7      8
8      7
313    314
314    313


Scoring

This is , so shortest answer in bytes wins.

Standard loopholes apply.

References

• May we take input as unary? May 4, 2017 at 10:12
• This would be, surprisingly, a lot easier if it was the other way around in some languages May 5, 2017 at 17:41
• @MistahFiggins That's well known enough that I'm pretty sure OP did it like this on purpose. May 5, 2017 at 20:51

QBIC, 15 10 bytes

:?a-(-1)^a


Saved a lot by using a different calculation.

Explanation:

:           Get a number frm the cmd line as var a
?           PRINT
a-(-1)^a   a decreased by 1 for when even, or -1 (adding one) when odd.


QBasic (and QBIC) need the parenteses around (-1), because code like -1^2 is otherwise seen as negate 1*1 = negate 1 = -1...

Pyth, 10 bytes

+Q?%Q2 1_1


Try it online

As simple as using modulo of input with 2 and the ternary operator.

• Golfed May 5, 2017 at 11:14
• May 5, 2017 at 11:15
• How long did I search for standard short circuit D: only found reverse.. nice one though May 5, 2017 at 11:16
• Welcome to PPCG, and welcome to Pyth. Hope you have a great time golfing in PPCG with Pyth. May 5, 2017 at 11:19

Actually, 6 bytes

;0Dⁿ@-


Explanation:

;0Dⁿ@-
;0Dⁿ    (-1)**n
@-  n - (above)


proc s n {expr $n%2?$n+1:$n-1}  demo Tcl, 24 based on @feersum's answer  proc s n {expr -(-$n^1)}


demo

Tcl, 23

If it is a program instead of a function I can shave one byte off.

puts [expr -(-$argv^1)]  demo Befunge-93, 11 bytes &:2%2*1-+.@  [Try it online!] Explanation: Stack representation: bottom [A, B, C top &: Pushes 2 copies of the input, N: [N, N 2% Mods the top copy by 2, so it's either 1 or 0: [N, (N % 2) 2* Multiplies that by 2, so that it's either 2 or 0: [N, ((N % 2) * 2) 1- Subtracts 1, so it's either 1 or -1: [N, (((N % 2) * 2) - 1) + Adds the 2 together. -1 for evens, and 1 for odds: [(N-1) or [(N+1) .@ Prints and ends  Befunge-98 Variant, 10 bytes This program can be trivially modified for Befunge-98 by removing the @, meaning the program will wrap around to the & and end: &:2%2*1-+.  Try it online! x86 machine code, 8 bytes Load register al with an 8 bit integer A8 01 (test al, 1; and whatever is in al with one, fast and small method for checking odd/even) 74 04 (jz 8; jump to even code if zf (zero flag, will be set if above instruction returns 0) is set) 04 02 (add al, 2; Two, because the next instruction will subtract one again, as if I incremented and jumped past the dec but shorter and faster) FE C8 (dec al; Explained above, this is the code that is jumped to if input is even)  If you want, here is some actual assembly in boot-sector format. Just assemble this with nasm filename.asm and run qemu-system-i386 filename to test it. Press a key, and you will see that it will be the next letter if the ascii code was odd, or previous if it was even. [ORG 0x7C00] start: xor ah, ah ;bios keyboard: get character (xoring a register with itself is a fast way to set it to 0 which is what we need) int 0x16 ; keyboard io bios interrupt mov ah, 0x0e ;bios graphics: display character (get ready) test al, 0x01 jz even add al, 2 even: dec al int 0x10 ; we already set al to 0E: display character, which takes a character from al and outputs that. jmp start ; just go back to the start and get another character times 510-($-) db 0 ; bootsector padding/signature
db 0x55
db 0xAA


Almost forgot, I should probably add the original assembly code that I used to create the bytes above

test al, 0x01
jz even

;odd code
even:
dec al


Clojure, 21 bytes

#(+(if(odd? %)1 -1)%)


It is quite difficult to get creative with this.

@REXX 35 Bytes

arg a
x=pos(".",a/2)-1
say a+abs(x)/x


Explanation: Explanation: There is no distinction in Rexx between numbers and strings. The action you perform is what defines the type. The "typing" applies just to that action and can change at any time.

So, after dividing the number by 2 we then search for the decimal point. Subtract 1 from its position giving -1 if it was not found and a value in the range 1-n if it was. (Note that 1/2 returns 0.5 and not .5 so pos can never be 1 and therefore x can never be 0).

Try it here

REXX functions and instructions

,,,, 4 bytes

_1^_


Yay.

Explanation

_1^_

input by command-line args
_     negate
1    push 1
^   pop input and 1 and push input ^ 1 (bitwise XOR)
_  negate
implicit output


f x|odd x=x+1|0<1=x-1


I have no idea why I'm doing this xD

Common Lisp, 24 bytes

(lambda(x)(- x(expt -1 x)))


Try it online!

J, 9 bytes

(22 b.)&1


22 b. is XOR. Port of Cyoce's ruby answer.

Try it online!

+1:_1:@.(2&|)

Try it online!

Keg, -hr, 7 bytes

:2%[;|⑨


Try it online!

Japt, 4 bytes

aJpU


Try it (includes all test cases)

aJpU     :Implicit input of integer U
a        :Absolute difference with
J       :  -1
pU     :  Raised to the power of U


Lua (40 bytes)

Using a table in order to avoid math.ceil or math.floor

function m(n)return n+({-1,1})[1+n%2]end


BRASCA, 7 bytes

ig{1_}n


Try it online!

Explanation

i       - Turn ASCII codepoints 48-57 to numbers 0-9
g      - Concatenate stack
{     - Decrement
1_   - XOR by 1
}  - Increment
n - Output as number


TI-Basic, 7 bytes

Ans-i^(2Ans


Takes input in Ans. Output is stored in Ans and is displayed.

Alternatively, 9 bytes:

Ans-cos(2πfPart(Ans/2


><> (Fish), 10 bytes

:2%2*+1-n;


Try it

Thunno 2, 5 bytes

Ṅ1Æ^Ṅ


Attempt This Online!

Explanation

Ṅ1Æ^Ṅ  # Implicit input
Ṅ  # -(        )
Ṅ      #   -input
Æ^   #         ^
1     #          1
# Implicit output
`