title: Return 1 if any bit is set otherwise 0 using a logical operation
body: <p>EDIT: the point is about answering the question not giving alternatives like we have meat instead of fish, I know there are simpler alternatives about it</p>
  
  <p><strong>Huge thanks to abelenky for his answer and to David Norris as well</strong></p>
  
  <hr>
  
  <p>For a specified number I would like to return 1 if that number is not 0 otherwise return 0 and without having to use a <code>for...loop</code>, <code>if</code> or <code>bool</code>.</p>
  
  <p>In <a href="http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/" rel="nofollow">this document</a> it does work fine except for 0 :</p>
  
  <blockquote>
    <p>Bit Hack #8. Right propagate the rightmost 1-bit.</p>
  
  <pre><code>y = x | (x-1) 
  </code></pre>
    
    <p>...</p>
    
    <p>This is not a clean hack, tho, as it produces all 1's if x = 0.</p>
  </blockquote>
  
  <p>The best thing I came up with is the following (for a byte type) :</p>
  
  <pre><code>for (int i = 0; i &lt; 256; i++)
  {
      int o =
          ((i &amp; (1 &lt;&lt; 7)) &gt;&gt; 7) |
          ((i &amp; (1 &lt;&lt; 6)) &gt;&gt; 6) |
          ((i &amp; (1 &lt;&lt; 5)) &gt;&gt; 5) |
          ((i &amp; (1 &lt;&lt; 4)) &gt;&gt; 4) |
          ((i &amp; (1 &lt;&lt; 3)) &gt;&gt; 3) |
          ((i &amp; (1 &lt;&lt; 2)) &gt;&gt; 2) |
          ((i &amp; (1 &lt;&lt; 1)) &gt;&gt; 1) |
          ((i &amp; (1 &lt;&lt; 0)) &gt;&gt; 0);
      Debug.Assert((i == 0 &amp; o == 0) || (i != 0 &amp;&amp; o == 1));
      Console.WriteLine(@"i {0:D3} o {1:D3}", i, o);
  }
  </code></pre>
  
  <p>However, looking at the disassembly it produces all this code :</p>
  
  <pre><code>        int o =
              ((i &amp; (1 &lt;&lt; 7)) &gt;&gt; 7) |
              ((i &amp; (1 &lt;&lt; 6)) &gt;&gt; 6) |
              ((i &amp; (1 &lt;&lt; 5)) &gt;&gt; 5) |
              ((i &amp; (1 &lt;&lt; 4)) &gt;&gt; 4) |
              ((i &amp; (1 &lt;&lt; 3)) &gt;&gt; 3) |
              ((i &amp; (1 &lt;&lt; 2)) &gt;&gt; 2) |
              ((i &amp; (1 &lt;&lt; 1)) &gt;&gt; 1) |
              ((i &amp; (1 &lt;&lt; 0)) &gt;&gt; 0);
  0000021f  mov         eax,dword ptr [ebp-6Ch] 
  00000222  mov         edx,80h 
  00000227  and         eax,edx 
  00000229  sar         eax,7 
  0000022c  mov         edx,dword ptr [ebp-6Ch] 
  0000022f  mov         ecx,40h 
  00000234  and         edx,ecx 
  00000236  sar         edx,6 
  00000239  or          eax,edx 
  0000023b  mov         edx,dword ptr [ebp-6Ch] 
  0000023e  mov         ecx,20h 
  00000243  and         edx,ecx 
  00000245  sar         edx,5 
  00000248  or          eax,edx 
  0000024a  mov         edx,dword ptr [ebp-6Ch] 
  0000024d  mov         ecx,10h 
  00000252  and         edx,ecx 
  00000254  sar         edx,4 
  00000257  or          eax,edx 
  00000259  mov         edx,dword ptr [ebp-6Ch] 
  0000025c  mov         ecx,8 
  00000261  and         edx,ecx 
  00000263  sar         edx,3 
  00000266  or          eax,edx 
  00000268  mov         edx,dword ptr [ebp-6Ch] 
  0000026b  mov         ecx,4 
  00000270  and         edx,ecx 
  00000272  sar         edx,2 
  00000275  or          eax,edx 
  00000277  mov         edx,dword ptr [ebp-6Ch] 
  0000027a  mov         ecx,2 
  0000027f  and         edx,ecx 
  00000281  sar         edx,1 
  00000283  or          eax,edx 
  00000285  mov         edx,dword ptr [ebp-6Ch] 
  00000288  mov         ecx,1 
  0000028d  and         edx,ecx 
  0000028f  or          eax,edx 
  00000291  mov         dword ptr [ebp-70h],eax 
  </code></pre>
  
  <p>Is there another way using some clever <strong>logical operation</strong> that would not produce that many instructions ?</p>
  
  <p>Note, the point here is not about reducing the number of instructions produced for the sake of performance, rather to know whether there as simpler way to do this.</p>
  
  <p>Some people hinted to convert the number to a boolean but unfortunately this is not possible in C#, there are also the <code>Convert.ToInt</code> and <code>Convert.ToBoolean</code> methods that could be used for this but I don't want to use them.</p>