Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 0 to 1 and Vice Versa

I was asked in an interview : how to convert 0 to 1 and 1 to 0. I answered :

  1. Simple if and switch
  2. Bit flipping.

Are there any other approach?

like image 363
javaguy Avatar asked May 06 '10 22:05

javaguy


1 Answers

Simple arithmetic:

x = 1 - x;

Actually, there are an infinite number of polynomials that will map 1 to 0 and vice versa. For example:

x = x * x * x * x * x - x * x * x * x + x * x - 2 * x + 1;
like image 96
Sean Avatar answered Oct 14 '22 16:10

Sean