[Question]
  [
# Task
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 [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest answer in bytes wins.
[Standard loopholes](https://codegolf.meta.stackexchange.com/q/1061/48934) apply.
# References
* [OEIS A103889](http://oeis.org/A103889)
  
[Answer]
# C, 20 bytes
```
f(x){return-(-x^1);}
```
[Try it online](https://tio.run/nexus/c-gcc#XVDtasMwDPzvpzhaCnaqbE2/NkjbF@kyCImzClZn2O4oDXn2zG6glOmHkE6606GhkVfVWe0v1qQyvX5mKu@HKZvq@1Jr7JyvuX05HYRg43Eu2UDGqrRfFaE6lRZJEppfJTqBEHHotfNV6bQ7FtijQ0ZYElaENWFD2BLeCO8ByVYxrdHnDy6PZdPacCiwFzkYO0jHN9028iGt8Ir/2HFRKBX253OF0U2MHxt0GzmZ1UgPmNUfZkJPFrkgPGtw1LiT@3sefxN8iH74Aw).
[Answer]
## [Stack Cats](https://github.com/m-ender/stackcats), 3 + 3 (`-n`) = 6 bytes
```
-*-
```
[Try it online!](https://tio.run/nexus/stackcats#@6@rpfv/v7Gh8X/dPAA "Stack Cats – TIO Nexus")
Needs the `-n` flag to work with numeric input and output.
### Explanation
Stack Cats is usually far from competitive, because of its limited set of commands (all of which are injections, and most of which are involutions) and because every program needs to have mirror symmetry. However, one of the involutions is to toggle the least-significant bit of a number, and we can offset the value with unary negation which also exists. Luckily, that gives us a symmetric program, so we don't need to worry about anything else:
```
-   Multiply the input by -1.
*   Toggle the least significant bit of the value (i.e. take it XOR 1).
-   Multiply the result by -1.
```
Input and output are implicit at the beginning and end of the program, because taking input and producing output is not a reversible operation, so they can't be commands.
[Answer]
# x86 Assembly, 9 bytes (for competing entry)
Everyone who is attempting this challenge in high-level languages is missing out on the *real* fun of manipulating raw bits. There are so many subtle variations on ways to do this, it's insane—and a lot of fun to think about. Here are a few solutions that I devised in 32-bit x86 assembly language.
I apologize in advance that this isn't the typical code-golf answer. I'm going to ramble a lot about the thought process of iterative optimization (for size). Hopefully that's interesting and educational to a larger audience, but if you're the TL;DR type, I won't be offended if you skip to the end.
The obvious and efficient solution is to test whether the value is odd or even (which can be done efficiently by looking at the least-significant bit), and then select between *n+1* or *n−1* accordingly. Assuming that the input is passed as a parameter in the `ECX` register, and the result is returned in the `EAX` register, we get the following function:
```
F6 C1 01  |  test  cl, 1                      ; test last bit to see if odd or even
8D 41 01  |  lea   eax, DWORD PTR [ecx + 1]   ; set EAX to n+1 (without clobbering flags)
8D 49 FF  |  lea   ecx, DWORD PTR [ecx - 1]   ; set ECX to n-1 (without clobbering flags)
0F 44 C1  |  cmovz eax, ecx                   ; move in different result if input was even
C3        |  ret
```
**(13 bytes)**
But for code-golf purposes, those `LEA` instructions aren't great, since they take 3 bytes to encode. A simple `DEC`rement of `ECX` would be much shorter (only one byte), but this affects flags, so we have to be a bit clever in how we arrange the code. We can do the decrement *first*, and the odd/even test *second*, but then we have to invert the result of the odd/even test.
Also, we can change the conditional-move instruction to a branch, which may make the code run more slowly (depending on how predictable the branch is—if the input alternates inconsistently between odd and even, a branch will be slower; if there's a pattern, it will be faster), which will save us another byte.
In fact, with this revision, the entire operation can be done in-place, using only a single register. This is great if you're inlining this code somewhere (and chances are, you would be, since it's so short).
```
    48     |  dec  eax          ; decrement first
    A8 01  |  test al, 1        ; test last bit to see if odd or even
    75 02  |  jnz  InputWasEven ; (decrement means test result is inverted)
    40     |  inc  eax          ; undo the decrement...
    40     |  inc  eax          ; ...and add 1
  InputWasEven:                 ; (two 1-byte INCs are shorter than one 3-byte ADD with 2)
```
**(inlined: 7 bytes; as a function: 10 bytes)**
But what if you did want to make it a function? No standard calling convention uses the same register to pass parameters as it does for the return value, so you'd need to add a register-register `MOV` instruction to the beginning or end of the function. This has virtually no cost in speed, but it does add 2 bytes. (The `RET` instruction also adds a byte, and there is a some overhead introduced by the need to make and return from a function call, which means this is one example where inlining produces both a speed *and* size benefit, rather than just being a classic speed-for-space tradeoff.) In all, written as a function, this code bloats to 10 bytes.
What else can we do in 10 bytes? If we care at all about performance (at least, *predictable* performance), it would be nice to get rid of that branch. Here is a branchless, bit-twiddling solution that is the same size in bytes. The basic premise is simple: we use a bitwise XOR to flip the last bit, converting an odd value into an even one, and vice versa. But there is one niggle—for odd inputs, that gives us *n-1*, while for even inputs, it gives us *n+1*— exactly opposite of what we want. So, to fix that, we perform the operation on a negative value, effectively flipping the sign.
```
8B C1     |  mov eax, ecx   ; copy parameter (ECX) to return register (EAX)
          |
F7 D8     |  neg eax        ; two's-complement negation
83 F0 01  |  xor eax, 1     ; XOR last bit to invert odd/even
F7 D8     |  neg eax        ; two's-complement negation
          |
C3        |  ret            ; return from function
```
**(inlined: 7 bytes; as a function: 10 bytes)**
Pretty slick; it's hard to see how that can be improved upon. One thing catches my eye, though: those two 2-byte `NEG` instructions. Frankly, two bytes seems like one byte too many to encode a simple negation, but that's the instruction set we have to work with. Are there any workarounds? Sure! If we `XOR` by -2, we can replace the second `NEG`ation with an `INC`rement:
```
8B C1     |  mov eax, ecx
          |
F7 D8     |  neg eax
83 F0 FE  |  xor eax, -2
40        |  inc eax
          |
C3        |  ret
```
**(inlined: 6 bytes; as a function: 9 bytes)**
Another one of the oddities of the x86 instruction set is [the multipurpose `LEA` instruction](https://stackoverflow.com/questions/1658294/whats-the-purpose-of-the-lea-instruction), which can do a register-register move, a register-register addition, offsetting by a constant, and scaling all in a single instruction!
```
8B C1        |  mov eax, ecx
83 E0 01     |  and eax, 1        ; set EAX to 1 if even, or 0 if odd
8D 44 41 FF  |  lea eax, DWORD PTR [ecx + eax*2 - 1]
C3           |  ret
```
**(10 bytes)**
The `AND` instruction is like the `TEST` instruction we used previously, in that both do a bitwise-AND and set flags accordingly, but `AND` actually updates the destination operand. The `LEA` instruction then scales this by 2, adds the original input value, and decrements by 1. If the input value was odd, this subtracts 1 (2×0 − 1 = −1) from it; if the input value was even, this adds 1 (2×1 − 1 = 1) to it.
This is a very fast and efficient way to write the code, since much of the execution can be done in the front-end, but it doesn't buy us much in the way of bytes, since it takes so many to encode a complex `LEA` instruction. This version also doesn't work as well for inlining purposes, as it requires that the original input value be preserved as an input of the `LEA` instruction. So with this last optimization attempt, we've actually gone backwards, suggesting it might be time to stop.
---
Thus, for the final competing entry, we have a 9-byte function that takes the input value in the `ECX` register (a semi-standard [register-based calling convention](https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_fastcall) on 32-bit x86), and returns the result in the `EAX` register (as with all x86 calling conventions):
```
           SwapParity PROC
8B C1         mov eax, ecx
F7 D8         neg eax
83 F0 FE      xor eax, -2
40            inc eax
C3            ret
           SwapParity ENDP
```
Ready to assemble with MASM; call from C as:
```
extern int __fastcall SwapParity(int value);                 // MSVC
extern int __attribute__((fastcall)) SwapParity(int value);  // GNU   
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 3 bytes
```
-*ạ
```
[Try it online!](https://tio.run/nexus/jelly#@6@r9XDXwv86h9tVHjWtcf//P9pQR8FIR8FYR8FER8FUR8FMR8FcR8ECKGJoDCJMYgE "Jelly – TIO Nexus")
Pseudocode: `abs((-1)**n - n)`
[Answer]
# Python3, ~~20~~ 18 bytes
```
lambda n:n-1+n%2*2
```
Pretty simple. First we calculate **n-1** and decide whether to add 2 to it, or not.
**If n is even** --> **n mod 2** will be 0, thus we'll add **2\*0** to **n-1**, resulting in **n-1**.
**If n is odd** --> **n mod 2** will be 1, thus we'll add **2\*1** to **n-1**, resulting in **n+1**.
I prefer an explanation that I made with MS paint & a laptop touchpad... [![Visual explanation](https://i.stack.imgur.com/Y8p0D.png)](https://i.stack.imgur.com/Y8p0D.png)
[Answer]
# Python, 16 bytes
```
lambda x:-(-x^1)
```
[Try it online!](https://tio.run/nexus/python3#S7ON@Z@TmJuUkqhQYaWroVsRZ6j5Py2/SCFTITNPoSgxLz1Vw1DH0FzTiouzoCgzr0RDKU1DNUXTSkE1RUlVI1NHIU0jU1NT8////wA)
[Answer]
# [MATL](https://github.com/lmendo/MATL), 7 bytes
```
Q:HePG)
```
This avoids any arithmetical operations. [Try it online!](https://tio.run/nexus/matl#@x9o5ZEa4K75/785AA "MATL – TIO Nexus")
### Explanation
Consider input `4` as an example.
```
Q    % Implicit input. Add 1
     % STACK: 5
:    % Range
     % STACK: [1 2 3 4 5]
He   % Reshape with 2 rows in column-major order. Pads with a zero if needed
     % STACK: [1 3 5;
               2 4 0]
P    % Flip vertically
     % STACK: [2 4 0;
               1 3 5]
G    % Push input again
     % STACK: [2 4 0;
               1 3 5], 4
)    % Index, 1-based, in column major order. Implicitly display
     % STACK: 3
  
```
[Answer]
## Python, 68 bytes
```
lambda x:[m.floor(x-m.cos(m.pi*x)) for m in [__import__('math')]][0]
```
In the spirit of a unique approach. The following graph shows the function (with purple dots representing the first 10 cases). It should in theory be possible to construct a solution for this question based on most (all?) periodic functions (e.g. sin, tan, sec). In fact, substituting cos for sec in the code as is should work.
[![Graph demonstrating function](https://i.stack.imgur.com/jXgXM.png)](https://i.stack.imgur.com/jXgXM.png)
[Answer]
# [Braingolf v0.1](https://github.com/gunnerwolf/braingolf), ~~11~~ 10 bytes
```
.1>2,%?+:-
```
[Try it online!](https://tio.run/nexus/python3#zVlNc9s2EL3rVyBuXZGNpEpJDx3JcjrTSWZ6ag@dXhTVw5CghIYiWYB05Gmnf93dBUB8kFAi9WL74CGBxe7bh90lsHrMeXUg4kEQdqgr3pCE7@5XIzmat2XaVFVh5jjN2pTq2bQqCpo2rCrNfEb/amFav1ViVifN3r7XlCdNxc0AayiX@lejUVokQhAhNUTyf7wcEdCYk7u7HW1A9nB3Fwla5BPCyowe5TwhLCdMsFI0SZnSSM5MiChYSrUAAdhNy0vSPNRUKogjY3nGpKirdwaqeGNfqto@0zqOR45KCXR2CuBohPARXRMJiabhD0uiYMsxV9dvvK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qE4C4Zffo@trwxbNkmCkfzv2F6rupNOUPBrmxpdqAXlJkZOH16CUt8YKeNIUG42DhYCc6YfxkkRXuEcSAZL/jZL8EJnc7is5XcODpmR/z2BMXdrMX7SPQZQbyJUvvZxfPrr74lPF0ZS3yLf/vLOuUOeXuD49sb4BqkL7pyVyCvn3AChB7IxuVm7B51BEbWHK3fsVFAHTtUe6o1Brc9DrlL3iHscLH35pTqBN0@fCHGSCX0ZXYX57l/ZZ0mWTTrGBBwCY@cNYjU@q5L3z/yhEuYXEtEedGxvz68Q3z0hTzlsIc/Y/VOQpZpeISwXU/jtE1IIX7vnwJ6EcTFx0yckTrQfngNxEsbFxP3xhMTV1afnQJyEcTFx10@ZqlX2LFIVYVxAXL9j4DmppobHjRfO8d64EjzZ3fTOabxqkLXQGeY2LBqQXBlJ1e2zlkfB1sjlzYzgTSnUxDirXRF7SvvXhl57wmtKnGpIhCNoeDvs7u5l5rZUnSbxsPeqeJD30ZHXw71RPVzTlYXIZhn2ccULdXnVMz9VnNO0Ia1IdhT7tuQmL5LdLbkRVctTeks26kGu3b4vEZgWWZK3rNlTTqY59vum6QyfWJmxFGJBkGafNFYPEyQhObYym4p8oJC4SUbwZ66J7nBO08@t/cATuPxXRS5bnp2Otpxho1n2p@e6p3xk8m4wOqCc7l3jLw3aDz2CvyYgZUoKP4D52GWMiH3VFhmaADBUWWX4DGDQja7JrdRK0zhcJge0oUb1ruqf6WZMoETUiek0/gQk4u9HsK9KFUnAggm4DnU@Q8a6W4/q@Du2beHRHrxDTq9VUzeHKM1eIFpjW@epJguSUaasZSP9DBvAANIRYKAHqwPVC0MMHx2GHYDHx8dp@jh7Nbl@s3i5XEymj4sf/gM "Python 3 – TIO Nexus") (Second argument is the Braingolf code, third argument is the input)
Saved a byte thanks to Neil
First ever competing braingolf answer :D
Explanation:
```
.            Duplicate the top of the stack
 1>          Push 1 to the bottom of the stack
   2         Push 2 to stack
    ,%       Pop last 2 items, mod them and push result
      ?      If last item > 0
       +     Add the 1 to the input
        :    Else
         -   Subtract the 1 from the input
             No semicolon in code so print last item
```
# [Braingolf v0.2](https://github.com/gunnerwolf/braingolf), 9 bytes
```
.2%?1+:1-
```
[Try it online!](https://tio.run/nexus/python3#zVlLj9s2EL77VzDbbi01tmsnBQr4sSlQJEBP7aHoxXEXikTZbLSSSkobL1r0r29nSIoPiU7sXnb3sJDI4cw3H2dG5Pgx59UdEQ@CsLu64g1J@P5@NZKjeVumTVUVZo7TrE2pnk2roqBpw6rSzGf0rxam9VslZnXSHOx7TXnSVNwMsIZyqX81GqVFIgQRUkMk/8fLEQGNObm93dMGZO9ubyNBi3xCWJnRo5wnhOWECVaKJilTGsmZCREFS6kWIAC7aXlJmoeaSgVxZCzPmBR19c5AFW/sS1XbZ1rH8chRKYHOTgEcjRA@omsiIdE0/GFJFGw55ur6jbcU3ukxpXVDfk@Klr7lvOJLV@hdUgiq9Co6adTROiE54wKAC5pWZSbt6VWdSORLrJQiAA8upx/vkyKSDxNScyoov6cT0HAvNQHN3aAHSC7YzncoALKEAj49OF3sVugQjAyXzOqqLmjeRPFwJc5FAXQiCA/8LljdgSxoqaRickMWJkR87ISgabI5A73EzfYHK70ISr/S0tZb38g5/gasnbfMGj3tqu/E/DTcgX1lQ29ehFITJSQxlVUjN0ABi@TEROrqdrBOuKBRWmUqI2uOsT/@FUZZuSfXYkyuiZxFM9IqWleVQI7pbcXKhLv6WgFWki83smJtXy93q0AEzJWstvlzWbfNUpvc4hCLSV5xwiAftUKsJzvFapkBEJlxK4lb8eqOQRrdQyVxh2CPKPjrDomGo6fOCBVpUnsyd23RMAnTW/kRiHXeWe5BEgyc9RbIndjIWoKvRVV56/ec0uzBHUEJWfBgcL7qtietWqn36spgSw8JNyPHTvrTASDA61rSbvYYmAEJfN0edaQdcasWEBL4gpw/IOc8Kfc0mk/sppmqDRuhYvUBEk7IOANk3SyxkxtVTPVrrE3gcmDP0QaQNmT8z5iAcfW8HDvqelR3y7L@IOx5w0rkt7ODHA7s7BzdxhW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3Kafm7tB57AJb4qctm67HS05Qz7xbLNPNet4SOTt4LRHcrpFjT@YKD90CP4owBSpqTw65ePXcaIOFRtkaEJAEOVVYbPAAbd6HrVSq00jcNlcoc21KjeVf1r24wJlIg6MZ3Gn4BE/BkI9lWpIglYMAHXoc5nyFh331GNe8e2LTzag3fI6bVqzuYQpdkLRGts6zzVZEEyypS1bKSfYQMYQDoCDPRgdaB6YYjho8OwA/D4@DhNH2eL9avrNy@X08fFD/8B "Python 3 – TIO Nexus") (Second argument is the Braingolf code, third argument is the input)
See above for explanation. Only difference is in Braingolf v0.2, the default behaviour of diadic operators, and the function of the `,` modifier, are reversed, meaning the 2 commas in the v0.1 answer are no longer needed.
[Answer]
# [Cubix](https://github.com/ETHproductions/cubix), ~~10~~ 9 bytes
```
cO1)I(//@
```
[Try it online](https://ethproductions.github.io/cubix/?code=Y08xKUkoLy9A&input=MzEz&speed=3)
### Explanation
Net version
```
    c O
    1 )
I ( / / @ . . .
. . . . . . . .
    . .
    . .
```
The executed characters are
```
I(1c)O@
I          Input
 (         Decrement
  1c       XOR with 1
    )      Increment
     O@    Output and exit
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 6 bytes
-2 thanks to Razetime
```
⊢-¯1*⊢
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24RHXYt0D6031ALS//8/6l2VdmiFxqPezRaaOsaGxkBsAgA "APL (Dyalog Unicode) – Try It Online")
Explanation:
```
 ⊢-¯1*⊢
   ¯1*⊢  ⍝ calculate ¯1*⍵ - for even ⍵: 1, for odd ⍵: ¯1.
 ⊢-      ⍝ subtract 1 for even, subtract ¯1 (add 1) for odd.
```
[Answer]
# JavaScript (ES6), ~~14~~ ~~13~~ ~~12~~ 10 bytes
```
n=>-(-n^1)
```
* 1 byte saved thanks to [Luke](https://codegolf.stackexchange.com/users/63774/luke).
* 2 bytes saved by porting [feersum's C solution](https://codegolf.stackexchange.com/a/118833/58974). (If that's frowned upon, please let me know and I'll roll back to my previous solution below)
---
## Try It
```
f=
n=>-(-n^1)
i.addEventListener("input",_=>o.innerText=f(+i.value))
```
```
<input id=i type=number><pre id=o>
```
---
[Answer]
# PHP, 15 bytes
```
<?=-(-$argn^1);
```
[Answer]
# Javascript, 17 12 bytes
```
x=>x-(-1)**x
```
```
f=x=>x-(-1)**x;
```
```
<input id=i oninput=o.innerText=f(this.value) type=number><pre id=o>
```
# Another approach, 10 bytes stolen from the C answer (sssshhh)
```
x=>-(-x^1)
```
```
f=x=>-(-x^1)
```
```
<input id=i oninput=o.innerText=f(this.value) type=number><pre id=o>
```
[Answer]
# [Cubix](https://github.com/ETHproductions/cubix), 11 bytes
```
u%2!I(/+@O<
```
[Try it online!](https://tio.run/nexus/cubix#@1@qaqToqaGv7eBv8/@/EQA "Cubix – TIO Nexus")
# Explanation
Net version:
```
    u %
    2 !
I ( / + @ O < .
. . . . . . . .
    . .
    . .
```
Characters are executed in following order:
```
I(2%!+O@
I        # Take a number as input
 (       # Decrement it
  2%     # Take the parity of the decremented number
         # (0 if the input is odd, 1 if it's even)
    !    # If that number is zero:
     +   #   Add 2
      O  # Output the number
       @ # Terminate the program
```
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 36 bytes
```
(({})(())){({}[()]<([{}])>)}{}({}{})
```
[Try it online!](https://tio.run/nexus/brain-flak#@6@hUV2rqaGhqalZDWRFa2jG2mhEV9fGatpp1lbXAoWA0v//GxoAAA "Brain-Flak – TIO Nexus")
I'm personally really happy with this answer because it is a lot shorter than what I would deem a traditional method of solving this problem.
## Explanation
The first bit of code
```
(({})(()))
```
converts the stack from just `n` to
```
n + 1
  1
  n
```
Then while the top of the stack is non zero, we decrement it and flip the sign of the number under it
```
{({}[()]<([{}])>)}
```
The we remove the zero and add the two remaining numbers
```
{}({}{})
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `r`, 4 bytes
```
‹ue+
```
[Try it Online!](https://lyxal.pythonanywhere.com?flags=r&code=%E2%80%B9ue%2B&inputs=5&header=&footer=)
Me and the boys porting risky be like
## Explained
```
‹ue+
‹    # input - 1
 u   # -1
  e  # ^^ ** ^
   + # ^ + input
```
[Answer]
# Python, 20 bytes
```
lambda n:n+(n%2or-1)
```
`n%2or-1` will return 1 if it's odd, but if it's even, `n%2` is "false" (0), so it instead returns -1. Then we simply add that to `n`.
### Previous solution, 23 bytes
```
lambda n:[n-1,n+1][n%2]
```
`n%2` calculates the remainder when `n` is divided by 2. If it's even, this returns 0, and element 0 in this list is `n-1`. If it's odd, this returns 1, and element 1 in this list is `n+1`.
[Answer]
# [Retina](https://github.com/m-ender/retina), 21 bytes
```
.+
$*
^11(?=(11)*$)
```
[Try it online!](https://tio.run/nexus/retina#U9VwT/ivp82losUVZ2ioYW@rYWioqaWiycX1/78hlxGXMZcJlymXGZc5lwWXsaExEJsAAA "Retina – TIO Nexus") My first Retina answer with two trailing newlines! Explanation: The first two lines convert from decimal to unary. The third and fourth lines subtract two from even numbers. The last line converts back to decimal, but adds one too.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 4 bytes
```
(1^(
```
[Try it online!](https://tio.run/nexus/05ab1e#@69hGKfx/78ZAA "05AB1E – TIO Nexus")
[Answer]
# Mathematica, ~~22~~ 19 bytes
Saved 3 bytes thanks to Greg Martin!
```
#-1[-1][[#~Mod~2]]&
```
### Previous answer, 22 bytes
```
#+{-1,1}[[#~Mod~2+1]]&
```
### Explanation (for previous answer)
Mathematica has the nice feature that operations such as arithmetic automatically thread over lists. 
In this case, we take `Mod[#,2]` which will return 0 or 1, but we need to add 1 because Mathematica lists are 1-indexed. If it's **even**, this comes out to 1, so `#-1` is returned. If it's **odd**, this comes out to 2, so `#+1` is returned. 
[Answer]
# [Wise](https://github.com/Wheatwizard/Wise), 8 bytes
```
-::^~-^-
```
[Try it online!](https://tio.run/nexus/wise#@69rZRVXpxun@///f0sA "Wise – TIO Nexus")
## Explanation
If this were the other way around, (decrement if odd, increment if even), it would be quite easy to do this.
We would just flip the last bit.
```
::^~-^
```
The fix here is that we flip the last bit while negative. The negative numbers are 1 off from the negation of the numbers `~` so this creates an offset resolving the problem.
So we just take out program and wrap it in `-`.
```
-::^~-^-
```
[Answer]
# R, 17 bytes
```
(n=scan())-(-1)^n
```
where `n=scan()` takes the digit value.
[Answer]
# [brainfuck](https://github.com/TryItOnline/brainfuck) (8-bit), 51 bytes
```
,[->>++>+<<<]+++[->>[-<++>]<[->++<]<]>>[>++<[-]]>-.
```
[Try it online!](https://tio.run/##SypKzMxLK03O/v9fJ1rXzk5b207bxsYmVltbG8SN1rUBisTaANna2jaxNrFAIRArWjc21k5X7/9/QwA "brainfuck – Try It Online")
Requires a fixed 8-bit cell size.
Decided to do something entirely on my own.
Inspired by [some meme ARM code I wrote to check if it is a multiple of 4 (screenshot)](https://i.stack.imgur.com/0iptM.png)
Instead of modulo, negate+xor, or binary AND, I shift out all the bits but the lowest to determine if it is even or odd.
```
// Checks parity by shifting n << 7.
// Requires 8-bit cells
,             // [  n    0    0   0]
              // copy loop, shifting one once
[->>++>+<<<]  // [  0    0 n<<1   n]
              // n <<= 6
+++           // c = 3
[-            // loop 3 times, shifting twice
    >>[-<++>] // [  c n<<1    0   n]
     <[->++<] // [  c    0 n<<1   n]
<]            // end loop
              // if even:
              //   tape[2]==0
              // else:
              //   tape[2]==0x80
>>[           // if tape[2] != 0:
    >++<[-]   //   n += 2
]
>-            // n -= 1
.             // print n
```
Equivalent C:
```
uint8_t parity(uint8_t n)
{
    uint8_t is_odd = n << 7; 
    if (is_odd != 0)
        n += 2;
    return n - 1;
}
```
The input is a byte from stdin, and the output is a byte printed to stdout. 0-255 only.
The demo uses ASCII arithmetic.
# [boolfuck](https://github.com/TryItOnline/boolfuck) (joke), 3 bytes
```
,+;
```
[Try it online!](https://tio.run/##S0oszvj/P7UiNVnByE7NkKs4tURBt4yroCgzryRNQV1H21pdwU6hJLW4RC8pPz8NLhFjoK5Qo6CfX1CiDxTPSStNzgYx0hBKgdIVFSkIDYbEafj/3wAA)
**For a real Boolfuck solution, see [here](https://codegolf.stackexchange.com/a/218203/94093)**
Operation works on 1-bit integers from stdin, prints to stdout. Obviously a joke.
[Answer]
# [Factor](https://factorcode.org/), 15 bytes
```
[ -1 over ^ - ]
```
[Try it online!](https://tio.run/##NY2xCsIwFEX3fsWlsy3WsYKrdOkiTqVCjK82aJP48qL06yO2uFw4cA53UFocp/OpaY81FLOaA4zDg9jSE5OScZlyiFaLcTasyMreKWSeSWT2bKwg0CuS1RSwz5q2RtCsRI9ZbqyPEmogx4eNEHZbdNXm2uOwHOIWPcrUoajg3sS4oECfchdlDf/dpH7eFw "Factor – Try It Online")
Uses `x-(-1)^x` method as in many other answers (specifically inspired by [the APL one](https://codegolf.stackexchange.com/a/216648/78410)).
In Factor, it is usually better (code-golf-wise) to minimize the number of conditionals, then minimize the number of functions overall (especially when the answer can be found with arithmetic, where all the functions have short names). The top answer beats a different arithmetic answer `x+x%2*2-1` (23 bytes)
```
[ dup 2 mod 2 * + 1 - ]
```
[Try it online!](https://tio.run/##NY07D4IwFIV3fsUJow8ijJi4GhYW40QYarlIo7T19jaGX48KcflOTnIevdLieL5eqvpcQjGrKcA4PIgtPTEqGRZkfbRajLNhtazsnULimUQmz8YKAr0iWU0Bx6SqSwTNSvSQpMb6KKEEUrzZCKE4oMl3txan5RBd9MjmZtECo@u@3GCLHHu0c@qirAP//qh@@Q8 "Factor – Try It Online")
and a conditional-based answer `x+(x.odd()?1:-1)` (21 bytes)
```
[ dup odd? 1 -1 ? + ]
```
[Try it online!](https://tio.run/##NY0xD4IwFIR3fsWFVSXiiImMhoXFOBGGWh7SCG19fY3h12OEuNzlku/L9UqL4@V@q@prAcWs5gDj8CK2NGJSMqyR9dFqMc6GbbKyTwqJZxKZPRsrCPSOZDUFnJOqLhA0K9FDkhrro4QCSPFhI4TTEU2@f7S4rIfooke2NGu7riuR45CjxA7tkroom/63J/Wjvw "Factor – Try It Online")
[Answer]
# [Risky](https://github.com/Radvylf/risky), 4 bytes
```
?+0--2?
```
[Try it online!](https://radvylf.github.io/risky?p=WyI_KzAtLTI_IiwiWzZdIiwwXQ)
### Explanation
```
?        The input number
 +0      plus 0
   -     minus
    -    -1
     2   to the power of
      ?  the input number
```
[Answer]
# Java 8, ~~16~~ 10 bytes
```
n->-(-n^1)
```
# Java 7, ~~34~~ 28 bytes
```
int c(int n){return-(-n^1);}
```
Boring ports of [*@feersum*'s amazing C answer](https://codegolf.stackexchange.com/a/118833/52210).  
[Try it here.](https://tio.run/nexus/java-openjdk#hc7NCoMwEATgu0@xx@SgkNo/8Bl68lgspKmUQFwl2RRE8uypgtduL3P5hmGM0yHAbSkAAmmyBrJFAiO2RLn4nqLHUpT4ULJJGWCKT7fW9vZntC8YtEXRkrf4vneg5bYG0M6B@qEaI1XTSuRQGKGkbH7rgdWa1SOrJ1bPrF5YvfKfVf3H99@pSPkL)
---
Old answers:
**Java 8, 16 bytes**
```
n->n%2<1?n-1:n+1
```
**Java 7, 34 bytes**
```
int c(int n){return--n%2>0?n:n+2;}
```
**Explanation (of old Java 7 answer):**
[Try it here.](https://tio.run/nexus/java-openjdk#hc5NCsIwEAXgfU8xGyFBWmzqHxb0BK66FBcxFgm009JMBCk5e2yhW8fN23xvhmca7RxcxwTAkSZrIFokMGJOlONQkx8wTXGlzpsLnnCtyhABev9opvJy8@7sE1ptUVQ0WHzd7qDl/BOg@jiq26zzlPUTUYPCiFzK8rcqVgtWt6zuWN2zemD1yG/Oiz@@7A5JiF8)
The answer above is a shorter variant of `int c(int n){return n%2<1?n-1:n+1;}` by getting rid of the space.
```
int c(int n){     // Method with integer parameter and integer return-type
  return--n%2>0?  //  If n-1 mod-2 is 1:
    n             //   Return n-1
   :              //  Else:
    n+2;          //   Return n+1
}                 // End of method
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 6 bytes
```
n ^1 n
```
[Try it online!](https://tio.run/nexus/japt#@5@nEGeokPf/vykA "Japt – TIO Nexus")
[Answer]
# [AWK](https://www.gnu.org/software/gawk/manual/gawk.html), 15 bytes
```
{$1+=$1%2*2-1}1
```
[Try it online!](https://tio.run/nexus/awk#@1@tYqhtq2KoaqRlpGtYa/j/v4KCoQIIcIEIIxjDGMYwgTFMYQwzGMMcxrCAMIwNjWEMsC4A "AWK – TIO Nexus")
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. :)
[Answer]
# Python, 20 bytes
```
lambda n:n+(n&1)*2-1
```
]