title: How to compare two bit values in C?
body: <p>I've been dabbling around a bit with C and I find that being able to directly manipulate bits is fascinating and powerful (and dangerous I suppose). I was curious as to what the best way would be to compare different bits in C would be. For instance, the number 15 is represented in binary as:</p>
  
  <pre><code>00001111
  </code></pre>
  
  <p>And the number 13 is represented as:</p>
  
  <pre><code>00001101
  </code></pre>
  
  <p>How would you compare what bits are different without counting them? It would be easy to use shifts to determine that 15 contains 4 1s and 13 contains 3 1s, but how would you output the difference between the two (ex that the 2^1 spot is different between the two)? I just can't think of an easy way to do this. Any pointers would be much appreciated!</p>
  
  <p>EDIT: I should have clarified that I know XOR is the right way to go about this problem, but I had an issue with implementation. I guess my issue was comparing one bit at a time (and not generating the difference per say). The solution I came up with is:</p>
  
  <pre><code> void compare(int vector1, int vector2) {         
       int count = 0; 
       unsigned int xor = vector1 ^ vector2;
  
       while (count &lt; bit_length) {
            if (xor % 2 == 1) { //would indicicate a difference
                 printf("%d ", count);
            }
            xor &gt;&gt;= 1; 
            count++;
        }  
   }
  </code></pre>