views:

409

answers:

9

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?

+13  A: 

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;
Sean
A: 

I guess you could do ABS(VAR - 1) but i think your approaches are more elegant

JP
+16  A: 

A few obvious possibilities:

!n
1-n
n^1
n==0
n!=1
n<1
Jerry Coffin
`!n` isn't reliably 1 => 0, 0 => 1 in a language agnostic way (in some languages, !0 = -1); in Java, it doesn't even work ("operator ! cannot be applied to int").
T.J. Crowder
@GregS:good point...I've removed that one, and added a couple of others.
Jerry Coffin
n^1, did you mean 0 ^ n ?
Totophil
1-n and -n + 1 are exactly the same in different ordering, kind of redundant.
Rubys
T.J. Crowder, if ! meant to be bitwise NOT then in Java it's represented by ~ (tilda)
Totophil
@Totophil No, 0 ^ 1 = 1 and 0 ^ 0 = 0, so it does not flip it correctly, whereas 1 ^ 1 = 0 and 0 ^ 1 = 1, so n ^ 1 is correct.
Brandon Bodnár
@T.J.Crowder:quite true -- almost no notation you can use is truly language agnostic. I don't think any of these is legal Scheme -- so of course I wrote in the one true language and ignored the "Java" tag as an obvious mistake. :-)
Jerry Coffin
Brandon Bodnár, that's not true. Please use a calculator if you must. Any number in power of 0 gives one, any number in the power of 1 gives itself.
Totophil
@Totophil , ^ is the xor operator in most languages.
Brandon Bodnár
@Brandon, ok I can see now, I owe you an apology: sorry. Then we really have two ways of flipping the number here: (n XOR 1) and (0 in power of n).
Totophil
Of these, only `1-n` and `n xor 1` are language agnostic in the sense that they deal with integer values. All others mix integers with boolean results, making them unsuitable for strongly-typed languages.
Schedler
@Totophil: if ^ is the power operator, then 0 ^ 0 is undefined (mathematically, see http://mathworld.wolfram.com/Power.html).
Greg Hewgill
@Greg, true in theory, nonetheless for most practical purposes absolute majority of implementations resolve 0 ^ 0 = 1. On the other hand n==0, n!=1 and n<1 are relational operations and in most modern languages will result in boolean value, not flip the 0 to 1 and back. This and the fact that boolean true and false won't always be represented in memory by 1 and 0 might lead to a very intresting discussion during the interview. The expressions would work in C because language creators took a shortcut in representing boolean values to simplify language syntax for compiler builders.
Totophil
You forgot modulus: (3 + n) % 2
Adam Gent
+3  A: 

they probably expected you to use bitwise NOT

Totophil
A: 

This should work for any two numbers...

(EDIT: looking at the other answers I may have misread the question... but I still like my answer :-)

public class X
{
    public static void main(final String[] argv)
    {
        int x = Integer.parseInt(argv[0]);
        int y = Integer.parseInt(argv[1]);

        x += y;
        y = x - y;
        x = x - y;

        System.out.println(x);
        System.out.println(y);
    }
}
TofuBeer
+10  A: 

Lookup table:

int[] swap = { 1, 0 };

And later:

x = swap[x];
Sean
What's wrong with this? The OP asked for any alternative methods.
Wallacoloo
Yeah, seriously.
Sean
This is a totally awesome answer. I like it because its the least mathematical. Also unlike the other answers this will fail if the domain and range are outside of 0-1 ala fail fast although not gracefully.+1
Adam Gent
+1  A: 

This one isn't the best, but it works:

pow(0, n);
Dinah
pow(0, 0) is mathematically undefined, see http://mathworld.wolfram.com/Power.html
Greg Hewgill
Is Stephen Wolfram some kind of authority on math? Take a look at this article to see a few differing opinions: http://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_zero_power
advs89
@Adam Doyle: Yeah, count on Wikipedia to present all points of view simultaneously. It's the internet equivalent of the [Total Perspective Vortex](http://en.wikipedia.org/wiki/Total_Perspective_Vortex#Total_Perspective_Vortex).
Greg Hewgill
I like this explanation http://betterexplained.com/articles/understanding-exponents-why-does-00-1/
Dinah
It depends on what kind of math you are doing and kind of comes down to the question is there a "GUT" for math which I believe Godel made doubtful. A good book on this is "Is God a Mathematician?" http://www.amazon.com/God-Mathematician-Mario-Livio/dp/074329405XI mean there are even some philosophers/mathematicians that think we should discount induction.
Adam Gent
+1  A: 

Some Trig: COS(PI * N)^2

In python

import math
math.cos(math.pi * n)  ** 2

I can't believe people forgot modulus:

(3 + n) % 2
Adam Gent
Also, `cos(n*pi/2)`
Greg Hewgill
+1: I love it !
Dinah
+1  A: 

Take a paper clip. Straighten it out. It's a 1. Bend it to meet its ends. It's a 0. To make it a 1, straighten it.

Paul Nathan
Genius! Need more of this.
fastcodejava
I don't think Java is powerful enough to do that.
Adam Gent
@Adam: You probably need to find an embedded robot lisp to really do the job right. Only Embedded Robot Lisps have the power to truly transform 0s into 1s and vice-versa.
Paul Nathan