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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## 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
- Function to check if a singly linked list is palindrome
- Python map function | Count total set bits in all numbers from 1 to n
- Count of N-digit numbers having digit XOR as single digit
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Queries to count characters having odd frequency in a range [L, R]
- Queries to find the XOR of an Array after replacing all occurrences of X by Y
- Count total set bits in all numbers from range L to R
- Generate first K multiples of N using Bitwise operators
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them