Convert 0 to 1 and Vice Versa

19,255

Solution 1

A few obvious possibilities:

!n
1-n
n^1
n==0
n!=1
n<1

Solution 2

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;

Solution 3

Lookup table:

int[] swap = { 1, 0 };

And later:

x = swap[x];

Solution 4

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.

Solution 5

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
Share:
19,255

Related videos on Youtube

javaguy
Author by

javaguy

Updated on April 21, 2022

Comments

  • javaguy
    javaguy about 2 years

    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?

    • BalusC
      BalusC about 14 years
      Didn't knew that Java is language agnostic. Cool.
    • Ken
      Ken about 14 years
      drelihan: It's a test to see if they're an architecture astronaut, or a programmer. They're looking to see if you draw a UML diagram and start talking about WSDL.
  • T.J. Crowder
    T.J. Crowder about 14 years
    !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").
  • Jerry Coffin
    Jerry Coffin about 14 years
    @GregS:good point...I've removed that one, and added a couple of others.
  • Rubys
    Rubys about 14 years
    1-n and -n + 1 are exactly the same in different ordering, kind of redundant.
  • Vlad Gudim
    Vlad Gudim about 14 years
    T.J. Crowder, if ! meant to be bitwise NOT then in Java it's represented by ~ (tilda)
  • Brandon Bodnar
    Brandon Bodnar about 14 years
    @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.
  • Jerry Coffin
    Jerry Coffin about 14 years
    @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. :-)
  • Vlad Gudim
    Vlad Gudim about 14 years
    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.
  • Brandon Bodnar
    Brandon Bodnar about 14 years
    @Totophil , ^ is the xor operator in most languages.
  • Vlad Gudim
    Vlad Gudim about 14 years
    @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).
  • Schedler
    Schedler about 14 years
    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.
  • Ponkadoodle
    Ponkadoodle about 14 years
    What's wrong with this? The OP asked for any alternative methods.
  • Greg Hewgill
    Greg Hewgill about 14 years
    @Totophil: if ^ is the power operator, then 0 ^ 0 is undefined (mathematically, see mathworld.wolfram.com/Power.html).
  • Vlad Gudim
    Vlad Gudim about 14 years
    @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.
  • Adam Gent
    Adam Gent about 14 years
    I don't think Java is powerful enough to do that.
  • Adam Gent
    Adam Gent about 14 years
    You forgot modulus: (3 + n) % 2
  • Paul Nathan
    Paul Nathan about 14 years
    @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.
  • Adam Gent
    Adam Gent about 14 years
    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
  • pkr
    pkr over 8 years
    Simplifies to x^5 - x^4 + x^2 - 2x + 1. But x^n = x for x = 0 or 1, so it further simplifies to x - x + x -2x + 1 which is just 1 - x. (In case anyone was unsure if they could actually use the second polynomial.)
  • fmnijk
    fmnijk over 4 years
    Which one is the best?
  • Jerry Coffin
    Jerry Coffin over 4 years
    @蔡易翔 lacking specific reason to do otherwise, I'd probably use 1-n.
  • TheEagle
    TheEagle over 2 years
    ARGH - why didn't I think of this ? +(1 - 0)