Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a simple trick for this problem:</p> <pre><code>bool IsPowerOfTwo(ulong x) { return (x &amp; (x - 1)) == 0; } </code></pre> <p>Note, this function will report <code>true</code> for <code>0</code>, which is not a power of <code>2</code>. If you want to exclude that, here's how:</p> <pre><code>bool IsPowerOfTwo(ulong x) { return (x != 0) &amp;&amp; ((x &amp; (x - 1)) == 0); } </code></pre> <h3>Explanation</h3> <p>First and foremost the bitwise binary &amp; operator from MSDN definition:</p> <blockquote> <p>Binary &amp; operators are predefined for the integral types and bool. For integral types, &amp; computes the logical bitwise AND of its operands. For bool operands, &amp; computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.</p> </blockquote> <p>Now let's take a look at how this all plays out:</p> <p>The function returns boolean (true / false) and accepts one incoming parameter of type unsigned long (x, in this case). Let us for the sake of simplicity assume that someone has passed the value 4 and called the function like so:</p> <pre><code>bool b = IsPowerOfTwo(4) </code></pre> <p>Now we replace each occurrence of x with 4:</p> <pre><code>return (4 != 0) &amp;&amp; ((4 &amp; (4-1)) == 0); </code></pre> <p>Well we already know that 4 != 0 evals to true, so far so good. But what about:</p> <pre><code>((4 &amp; (4-1)) == 0) </code></pre> <p>This translates to this of course:</p> <pre><code>((4 &amp; 3) == 0) </code></pre> <p>But what exactly is <code>4&amp;3</code>?</p> <p>The binary representation of 4 is 100 and the binary representation of 3 is 011 (remember the &amp; takes the binary representation of these numbers). So we have:</p> <pre><code>100 = 4 011 = 3 </code></pre> <p>Imagine these values being stacked up much like elementary addition. The <code>&amp;</code> operator says that if both values are equal to 1 then the result is 1, otherwise it is 0. So <code>1 &amp; 1 = 1</code>, <code>1 &amp; 0 = 0</code>, <code>0 &amp; 0 = 0</code>, and <code>0 &amp; 1 = 0</code>. So we do the math:</p> <pre><code>100 011 ---- 000 </code></pre> <p>The result is simply 0. So we go back and look at what our return statement now translates to:</p> <pre><code>return (4 != 0) &amp;&amp; ((4 &amp; 3) == 0); </code></pre> <p>Which translates now to:</p> <pre><code>return true &amp;&amp; (0 == 0); </code></pre> <pre><code>return true &amp;&amp; true; </code></pre> <p>We all know that <code>true &amp;&amp; true</code> is simply <code>true</code>, and this shows that for our example, 4 is a power of 2.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload