# 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? – Kritixi Lithos May 4 '17 at 10:12
• This would be, surprisingly, a lot easier if it was the other way around in some languages – MildlyMilquetoast May 5 '17 at 17:41
• @MistahFiggins That's well known enough that I'm pretty sure OP did it like this on purpose. – Ørjan Johansen May 5 '17 at 20:51

# CJam, 7 6 bytes

{(1^)}


Try it online!

-1 thanks to Martin Ender. ...............OOOO

# C#, 34 11 bytes

n=>-(-n^1);


# Swift, 29 bytes

var f={(i)->Int in i-1+i%2*2}


Try it here

# AWK, 15 bytes

{$1+=$1%2*2-1}1


Try it online!

I could save 1 byte by using (-1)^$1, but then it would be pretty much the same as everyone else, and this is what I came up with before looking at other answers. :) # Fourier, 17 bytes I~x^ox%2{0}{@xvo}  Try it on FourIDE! Explanation: I~x - Stores input in a variable called x x^o - Increments x and outputs x%2{0}{ } - If x mod 2 == 0, do code inside brackets @ - Clear screen xvo - Decrements x and outputs  # 05AB1E, 7 bytes D1(sm-Ä  Try it online! Explanation: D1(sm-Ä Input (implicit). Is 3 for example. Stack: [3] D Duplicate input. Stack: [3, 3] 1 Push 1. Stack: [3, 3, 1] ( Push opposite of top of stack. Stack: [3, 3, -1] s Swap the top 2 items on the stack. Stack: [3, -1, 3] m Push -1**3. Stack: [3, -1] - Subtract. Stack: [4]. Ä Absolute value. Stack: [4]. Implicit output.  ## Excel, 11 bytes =A1-(-1)^A1  Assumes input in A1. # Groovy, 17 bytes print x%2?x+1:x-1  Just substitute x for desired number. Try it online here -> https://tio.run/nexus/groovy and copy the code. ## SAS, 72 bytes %macro t(n);%put%eval(&n+%sysfunc(ifn(%sysfunc(mod(&n,2)),1,-1)));%mend;  # dc, 10 ?d2%2*+1-p  Uses the same formula as @TuukkaX's python answer. ## Pyth, 4 bytes I'm new to Pyth, so please tell me if there are any things I can golf. _x1_  Explanation:  _ Negate input x1 Bitwise XOR with 1 _ Negate  • Welcome to Pyth! Hope you'll have a great time golfing in Pyth. – Leaky Nun May 5 '17 at 6:02 ## Excel VBA 27 bytes [a2]=iif(n mod 2=0,n-1,n+1)  • I don't think taking input from a variable is legal on PPCG (the question's worded a bit weirdly, but I don't think that permits it). You'd have to write a program or function. (Also, although it's optional, I recommend writing an explanation or brief description of how your code works; posts like this one which are just a code block have a tendency to set off the low-quality-posts filter, which can cause trouble sometimes.) – user62131 May 5 '17 at 11:11 ## 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 – Leaky Nun May 5 '17 at 11:14 • – Leaky Nun May 5 '17 at 11:15 • How long did I search for standard short circuit D: only found reverse.. nice one though – kalsowerus May 5 '17 at 11:16 • Welcome to PPCG, and welcome to Pyth. Hope you have a great time golfing in PPCG with Pyth. – Leaky Nun May 5 '17 at 11:19 # Actually, 6 bytes ;0Dⁿ@-  Explanation: ;0Dⁿ@- ;0Dⁿ (-1)**n @- n - (above)  # Tcl, 30 proc s n {expr$n%2?$n+1:$n-1}


demo

 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

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.

# J-uby, 12 bytes

:-@|:^&1|:-@


## Explanation

:-@|            negate n
:^&1|       XOR it with 1
:-@    negate that


With a change I just pushed to GitHub (inspired by this question, but overall useful), it becomes 10 bytes:

:-|:^&1|:-


# Chip, 61 bytes

  * *
A~#-#a
B~#~#b
C~#~#c
D~#~#d
E~#~#e
F~#~#f
G~#~#g
H~#~#h


Try it online!

Since Chip natively handles only byte-sized integers, this solution reads in and writes out bytes. The TIO is set up to use the \x00 notation for this.

This Chip solution uses a straightforward approach where the columns are the operations, and the rows are for each bit:

Read in an octet
|Invert all bits
||Increment
|||Invert all bits except the lowest bit
||||Increment
|||||Output the octet
||||||

* *
A~#-#a
B~#~#b
C~#~#c
D~#~#d
E~#~#e
F~#~#f
G~#~#g
H~#~#h


# Klein, 21 + 3 bytes, (non-competing)

Uses the 000 topology.

:(1-(\))++@
1-+:?\)-(


## Explanation

The first bit executed is :(1-(. This puts a copy of the input and a -1 in the scope to be recalled later. This is just required set up for future computation.

The two mirrors \\ then move the ip down a level into the main loop. The main loop, unfolded, is:

)-(1-+:?\


This recalls the first number of the scope and flips its sign then decrements the tos. It will continue to flip the sign back and forth until it reaches zero. This way it will be -1 if our input was even and 1 if it was odd. Once the counter has reached zero ? stops jumping over the mirror and the pointer wraps around to the top. Now the mirror we used earlier deflects the ip to the right causing it to run the code ))++@.

This code recalls the two things from the scope (the copy of the input and the number we have been flipping) with )). And adds the top three items with ++. Since the counter is at zero the result is just the sum of the number we built and the input. Since it can be either -1 or 1 depending on the parity of the input this will flip the sign of the input.

Finally @ ends execution.

@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

# braingasm, 8 bytes

;o[++]-:


; reads a number, o[++] increments twice if the number is odd, -decrements once, : prints.

edit: changed to p to o to retrofit to a breaking change in the language.

# ,,,, 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!

# Ly, 8 bytes

n:2%2*+,


Try it online!

Explanation:

n:2%2*+,

n        # take input
:2%     # modulo two
2*   # multiply by two
`