title: Change AND to NAND logic doesn't work in C anymore
body: <p>I've tried to find an algorithm which...</p>
  
  <ul>
  <li>...returns 1 (if x == 2^n) //2^n is "2 to the power of n" not xor.</li>
  <li>...returns 0 (if x != 2^n &amp;&amp; x == 0) //2^n is "2 to the power of n" not xor.</li>
  </ul>
  
  <p>I may assume that my machine works with:</p>
  
  <ul>
  <li>two's complement,</li>
  <li>32-bit representation of integers,</li>
  <li>performs right shifts arithmetically,</li>
  <li>unpredictable behavior when shifting an integer by more than the word size.</li>
  </ul>
  
  <p>I'm also only allowed to use the following operators: <code>! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</code></p>
  
  <p>This is what I got so far:</p>
  
  <pre><code>int isPower2(int x) {
    return ( !(x ^ (x &amp; ~x+1)) )); // But this returns 1 if x == 0.
  }
  </code></pre>
  
  <p>To get this code working correctly I changed it to <code>((x ^ 0) &amp;&amp; ( !(x ^ (x &amp; ~x+1)</code>. But I'm not allowed to use <code>&amp;&amp;</code>. So I have to build an <code>AND</code> out of <code>NANDs</code> using the <code>~</code> and <code>&amp;</code> operator.</p>
  
  <p>But I know, that I can build every possible GATE with <code>NANDs</code> (I only need <code>~</code> and <code>&amp;</code> for it).</p>
  
  <p>So... <code>X AND Y</code> is equal to <code>(X NAND Y) NAND (X NAND Y)</code> - <a href="http://de.wikipedia.org/wiki/NAND-Gatter" rel="nofollow noreferrer">NAND logic</a></p>
  
  <pre><code>X := (x ^ 0)
  Y := !(x ^ (x &amp; ~x+1))
  </code></pre>
  
  <p>But changing <code>((x ^ 0) &amp;&amp; ( !(x ^ (x &amp; ~x+1)) ))</code> into <code>~(~((x ^ 0) &amp; !(x ^ (x &amp; ~x+1)) ) &amp; ~((x ^ 0) &amp; !(x ^ (x &amp; ~x+1))))</code> doesn't seem to do the trick.</p>
  
  <p>Edit: I added some extra information, hope my problem is clearer now.</p>