title: bit manipulation C allEvenBits
body: <p>I have to use bitwise manipulations to accomplish something.
  This is what the question asks of me:</p>
  <pre><code>/* 
   * allEvenBits - return 1 if all even-numbered bits in word set to 1
   * Examples allEvenBits(0xFFFFFFFE) = 0, allEvenBits(0x55555555) = 1
   *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;
   *   Max ops: 12
   *   Rating: 2
   */
  </code></pre>
  <p>This is what I've worked out:</p>
  <pre><code>int allEvenBits(int x) {
  int y, z, p, q, r, s, t, u, k;
  y=x;
  z=y&amp;0x5555;
  y&gt;&gt;16;
  p=y&amp;z;
  q=p&amp;0x055;
  p&gt;&gt;8;
  r=q&amp;p;
  s=r&amp;0x05;
  r&gt;&gt;4;
  t=s&amp;r;
  u=t&amp;0x01;
  t&gt;&gt;2;
  k=u&amp;t;
  return k;
  }
  </code></pre>
  <p>But everytime I run this to test whether the function holds for all possible values of 32 bit integers, I get this error message:</p>
  <blockquote>
  <p>ERROR: Test allEvenBits(-2147483647[0x80000001]) failed...</p>
  <p>...Gives 1[0x1]. Should be 0[0x0]</p>
  </blockquote>
  <p>This, even though I'm ANDing all the even bits at each stage, so theres no way I would get the function to return a 1 when the final manipulation yeilds a 0. I tried math with a smaller 8 bit number of a similar form, it gives the right answer, so I don't understand what is happening wrong with this implementation.</p>