# Write a function that returns 2 for input 1 and returns 1 for 2

Write a function which returns 1 that 2 is passed and return 2 when 1 is passed.

Source: Adobe Interview Experience | Set 19 (For MTS)

A **simple solution **is to compare the passed value with 1 and 2.

`int` `invert(` `int` `x) ` `{ ` ` ` `if` `(x == 1) ` `return` `2; ` ` ` `else` `return` `1; ` `} ` |

*chevron_right*

*filter_none*

Another solution is to **use subtraction**

`int` `invertSub(` `int` `x) ` `{ ` ` ` `return` `(3-x); ` `} ` |

*chevron_right*

*filter_none*

We can also **use bitwise xor operator**.

`int` `invertXOR(` `int` `x) ` `{ ` ` ` `return` `(x ^ 1 ^ 2); ` `} ` |

*chevron_right*

*filter_none*

This article is contributed by **Anuj**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Write your own atoi()
- Write your own strcmp that ignores cases
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Python map function | Count total set bits in all numbers from 1 to n
- Count number of common elements between two arrays by using Bitset and Bitwise operation
- Sum of Bitwise OR of all pairs in a given array
- Maximum subset sum such that no two elements in set have same digit in them
- Count pairs of elements such that number of set bits in their AND is B[i]
- Number of 0s and 1s at prime positions in the given array
- Find uncommon characters of the two strings | Set 2
- Fibonacci Power
- Find N distinct numbers whose bitwise Or is equal to K
- Find a sub matrix with maximum XOR