title: Bitwise operations on boolean values
body: <p>From my knowledge, bitwise operators perform a check on all corresponding bits, as in:</p>
  
  <pre><code>echo 64 | 32;   //prints 96
  echo 'a' &amp; 'b'; //prints `
  </code></pre>
  
  <p>While conditional <code>&amp;&amp;</code> and <code>||</code> operators perform operations on boolean values:</p>
  
  <pre><code>echo (int)(true &amp;&amp; false); //prints: 0
  echo (int)(true || false); //prints: 1
  </code></pre>
  
  <p>When I (in my head) want to predict the result of a bitwise operation, I first convert the values into their binary representation (which depends on the datatype). After this, I compare it bit-for-bit, and convert the result into the suitable output type (which I suppose is determined by the operands). Although at one point, I tried doing the same with boolean values, which (from my knowledge) only consists of one bit in memory, making <code>true</code> corresponding to <code>1₂</code>, and making <code>false</code> corresponding to <code>0₂</code> (in binary). Performing bitwise operations on these values should therefore produce a similar result as with <code>&amp;&amp;</code> and <code>||</code>, right? To show you what I mean:</p>
  
  <pre><code>true &amp; false    =&gt;      1₂ &amp; 0₂      =&gt;      0₂     =&gt;     false
  true | false    =&gt;      1₂ | 0₂      =&gt;      1₂     =&gt;     true
  ~true           =&gt;      ~1₂          =&gt;      0₂     =&gt;     false
  </code></pre>
  
  <p>(Not including <code>xor</code>, as there's no corresponding conditional boolean operator.)</p>
  
  <p>To me, it looks like the behaviour should be really equivalent to conditional operators:</p>
  
  <pre><code>true &amp;&amp; false   =&gt;      false
  true || false   =&gt;      true
  !true           =&gt;      false
  </code></pre>
  
  <p>So therefore, I set this code up to test it:</p>
  
  <pre><code>    echo "true AND false: " . ((true &amp;&amp; false) ? "1" : "0") . "&lt;br /&gt;\n";
      echo "true OR false: " . ((true || false) ? "1" : "0") . "&lt;br /&gt;\n";
      echo "NOT true: " . ((!true) ? "1" : "0") . "&lt;br /&gt;\n";
  
      echo "&lt;br /&gt;\n";
  
      echo "true BITAND false: " . ((true &amp; false) ? "1" : "0") . "&lt;br /&gt;\n";
      echo "true BITOR false: " . ((true | false) ? "1" : "0") . "&lt;br /&gt;\n";
      echo "BITNOT true: " . ((~true) ? "1" : "0") . "&lt;br /&gt;\n";
  </code></pre>
  
  <p>It gave me the following output:</p>
  
  <blockquote>
    <p>true AND false: 0<br>
    true OR false: 1<br>
    NOT true: 0   </p>
    
    <p>true BITAND false: 0<br>
    true BITOR false: 1    </p>
    
    <p>Fatal error: Unsupported operand types in C:\Abyss Web Server\htdocs\handler.php on line 21</p>
  </blockquote>
  
  <p>So from this, I have two questions:</p>
  
  <ol>
  <li>What's the point with <code>&amp;&amp;</code> and <code>||</code>, if we (as it seems) can use <code>&amp;</code> and <code>|</code> on boolean values instead?</li>
  <li>Why can't I do <code>~true</code> (or with other words, why aren't boolean values supported)? To me, it sounds logical that <code>~true</code> returns <code>false</code>.</li>
  </ol>
  
  <p>I did come up with one thing, being that <code>&amp;&amp;</code> and <code>||</code> will (sometimes) cast the values into <code>bool</code> and thereafter return the correct results, if we (by mistake) would happen to pass a value that's not of type <code>bool</code>. But to solve that, can't we just do a cast first? Such as:</p>
  
  <pre><code>if ((bool)$foo &amp; (bool)$bar) { ...
  </code></pre>
  
  <p>It feels like I'm missing a major piece here that changes everything... But just in case I didn't, I included as much info as I could. Could someone make me understand this a bit better, by answering my two questions? I'm pretty confused at this point, and I've been thinking about this for quite some time.</p>