title: Bitwise Operation on a byte and an int
body: <p>I have a byte array represented as </p>
  
  <pre><code>char * bytes = getbytes(object); //some api function
  </code></pre>
  
  <p>I want to check whether the bit at some position x is set.</p>
  
  <p>I've been trying this</p>
  
  <pre><code>int mask = 1 &lt;&lt; x % 8;
  y= bytes[x&gt;&gt;3] &amp; mask;
  </code></pre>
  
  <p>However y returns as all zeros? What am I doing incorrectly and is there an easier way to check if a bit is set?</p>
  
  <p>EDIT: </p>
  
  <p>I did run this as well. It didn't return with the expected result either. </p>
  
  <pre><code>  int k = x &gt;&gt; 3;
     int mask = x % 8;
      unsigned char byte = bytes[k];
     return (byte &amp; mask);
  </code></pre>
  
  <p>it failed an assert true ctest I ran. Byte and Mask at this time where "0002" and 2 respectively when printed from gdb. </p>
  
  <p>edit 2: This is how I set the bits in the first place. I'm just trying to write a test to verify they are set. </p>
  
  <p><code>unsigned long x = somehash(void* a);</code></p>
  
  <p><code>unsigned int mask = 1 &lt;&lt; (x % 8);</code></p>
  
  <pre><code>unsigned int location = x &gt;&gt; 3;
  char* filter = getData(ref);
  
  filter[location] |= mask;
  </code></pre>