Write a function that returns 2 for input 1 and returns 1 for 2 - GeeksQuiz
Write a function which returns 1 that 2 is passed and return 2 when 1 is passed.
Read full article from Write a function that returns 2 for input 1 and returns 1 for 2 - GeeksQuiz
Write a function which returns 1 that 2 is passed and return 2 when 1 is passed.
int invert( int x) { if (x == 1) return 2; else return 1; } |
Another solution is to use subtraction
int invertSub( int x) { return (3-x); } |
We can also use bitwise xor operator.
int invertXOR( int x) { return (x ^ 1 ^ 2); } |