title: How can I check if a bit value is equal to 1?
body: <p>I have a running OpenCL kernel. How can I check if a bit at a selected position in a variable equals to one or not?</p>
  <p>For example, I found that in C# we can convert <code>uint</code> to string containing bits using code <a href="https://stackoverflow.com/questions/3702216/how-to-convert-integer-to-binary-string-in-c">like this</a>:</p>
  <pre><code>// Here we will store our bit
  
  string bit;
  
  // Unsigned integer that will be converted
  
  uint hex = 0xfffffffe;
  
  // Perform conversion and store our string containing bits
  
  string str = uint2bits(hex);
  
  // Print all bits &quot;11111111111111111111111111111110&quot;
  
  Console.WriteLine(str);
  
  // Now get latest bit from string
  
  bit = str.Substring(31, 1);
  
  // And print it to console
  
  Console.WriteLine(bit);
  
  // This function converts uint to string containing bits
  
  public static string uint2bits(uint x) {
      char[] bits = new char[64];
      int i = 0;
      while (x != 0) {
          bits[i++] = (x &amp; 1) == 1 ? '1' : '0';
          x &gt;&gt;= 1;
      }
      Array.Reverse(bits, 0, i);
      return new string(bits);
  }
  </code></pre>
  <p>How to make inside kernel like in code above?</p>
  <pre><code>__kernel void hello(global read_only uint* param) {
  
  /*
  
  Do something with &quot;param&quot;
  
  ...
  
  */
  
  int bit;
  int index = 31;
  
  /*
  
  Store bit value (&quot;0&quot; or &quot;1&quot;) at variable &quot;bit&quot;
  
  ...
  
  */
  
  if (bit == 1) {
  
  // ...
  
  } else {
  
  // ...
  
  }
  
  // Or maybe something more easy to check a bit value at &quot;index&quot; in &quot;param&quot; ... ?
  
  }
  </code></pre>
  <p>Where I can read more about that?</p>