Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8939

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Bitwise Operators</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.assignment.html">Assignment Operators</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.comparison.html">Comparison Operators</a></div>
 <div class="up"><a href="language.operators.html">Operators</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.operators.bitwise" class="sect1">
   <h2 class="title">Bitwise Operators</h2>
   <p class="simpara">
    Bitwise operators allow evaluation and manipulation of specific
    bits within an integer.
   </p>
   <table class="doctable table">
    <caption><strong>Bitwise Operators</strong></caption>
    
     <thead>
      <tr>
       <th>Example</th>
       <th>Name</th>
       <th>Result</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><strong class="userinput"><code>$a &amp; $b</code></strong></td>
       <td>And</td>
       <td>Bits that are set in both <var class="varname"><var class="varname">$a</var></var> and <var class="varname"><var class="varname">$b</var></var> are set.</td>
      </tr>

      <tr>
       <td><strong class="userinput"><code>$a | $b</code></strong></td>
       <td>Or (inclusive or)</td>
       <td>Bits that are set in either <var class="varname"><var class="varname">$a</var></var> or <var class="varname"><var class="varname">$b</var></var> are set.</td>
      </tr>

      <tr>
       <td><strong class="userinput"><code>$a ^ $b</code></strong></td>
       <td>Xor (exclusive or)</td>
       <td>
        Bits that are set in <var class="varname"><var class="varname">$a</var></var> or <var class="varname"><var class="varname">$b</var></var> but not both are set.
       </td>
      </tr>

      <tr>
       <td><strong class="userinput"><code>~ $a</code></strong></td>
       <td>Not</td>
       <td>
        Bits that are set in <var class="varname"><var class="varname">$a</var></var> are not set, and vice versa.
       </td>
      </tr>

      <tr>
       <td><strong class="userinput"><code>$a &lt;&lt; $b</code></strong></td>
       <td>Shift left</td>
       <td>
        Shift the bits of <var class="varname"><var class="varname">$a</var></var> <var class="varname"><var class="varname">$b</var></var> steps to the left (each step means
        &quot;multiply by two&quot;)
       </td>
      </tr>

      <tr>
       <td><strong class="userinput"><code>$a &gt;&gt; $b</code></strong></td>
       <td>Shift right</td>
       <td>
        Shift the bits of <var class="varname"><var class="varname">$a</var></var> <var class="varname"><var class="varname">$b</var></var> steps to the right (each step means
        &quot;divide by two&quot;)
       </td>
      </tr>

     </tbody>
    
   </table>

   <p class="para">
    Bit shifting in PHP is arithmetic.
    Bits shifted off either end are discarded.
    Left shifts have zeros shifted in on the right while the sign
    bit is shifted out on the left, meaning the sign of an operand
    is not preserved.
    Right shifts have copies of the sign bit shifted in on the left,
    meaning the sign of an operand is preserved.
   </p>
   <p class="para">
    Use parentheses to ensure the desired
    <a href="language.operators.precedence.html" class="link">precedence</a>.
    For example, <em>$a &amp; $b == true</em> evaluates
    the equivalency then the bitwise and; while
    <em>($a &amp; $b) == true</em> evaluates the bitwise and
    then the equivalency.
   </p>
   <p class="para">
    Be aware of data type conversions. If both the left-hand and
    right-hand parameters are strings, the bitwise operator will
    operate on the characters&#039; ASCII values.
   </p>
   <p class="para">
    <div class="informalexample">
     <p class="para">
      <pre class="literallayout">
PHP&#039;s error_reporting ini setting uses bitwise values,
providing a real-world demonstration of turning
bits off. To show all errors, except for notices,
the php.ini file instructions say to use:
<strong class="userinput"><code>E_ALL &amp; ~E_NOTICE</code></strong>
      </pre>
     </p>
     <p class="para">
      <pre class="literallayout">
This works by starting with E_ALL:
<span class="computeroutput">00000000000000000111011111111111</span>
Then taking the value of E_NOTICE...
<span class="computeroutput">00000000000000000000000000001000</span>
... and inverting it via <em>~</em>:
<span class="computeroutput">11111111111111111111111111110111</span>
Finally, it uses AND (&amp;) to find the bits turned
on in both values:
<span class="computeroutput">00000000000000000111011111110111</span>
      </pre>
     </p>
     <p class="para">
      <pre class="literallayout">
Another way to accomplish that is using XOR (<em>^</em>)
to find bits that are on in only one value or the other:
<strong class="userinput"><code>E_ALL ^ E_NOTICE</code></strong>
      </pre>
     </p>
    </div>
   </p>
   <p class="para">
    <div class="informalexample">
     <p class="para">
      <pre class="literallayout">
error_reporting can also be used to demonstrate turning bits on.
The way to show just errors and recoverable errors is:
<strong class="userinput"><code>E_ERROR | E_RECOVERABLE_ERROR</code></strong>
      </pre>
     </p>
     <p class="para">
      <pre class="literallayout">
This process combines E_ERROR
<span class="computeroutput">00000000000000000000000000000001</span>
and
<span class="computeroutput">00000000000000000001000000000000</span>
using the OR (<em>|</em>) operator
to get the bits turned on in either value:
<span class="computeroutput">00000000000000000001000000000001</span>
      </pre>
     </p>
    </div>
   </p>
   <p class="para">
    <div class="example" id="example-118">
     <p><strong>Example #1 Bitwise AND, OR and XOR operations on integers</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;Ignore&nbsp;the&nbsp;top&nbsp;section,<br />&nbsp;*&nbsp;it&nbsp;is&nbsp;just&nbsp;formatting&nbsp;to&nbsp;make&nbsp;output&nbsp;clearer.<br />&nbsp;*/<br /><br /></span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'(%1$2d&nbsp;=&nbsp;%1$04b)&nbsp;=&nbsp;(%2$2d&nbsp;=&nbsp;%2$04b)'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&nbsp;%3$s&nbsp;(%4$2d&nbsp;=&nbsp;%4$04b)'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;&lt;&lt;&lt;EOH<br /></span><span style="color: #DD0000">&nbsp;---------&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---------&nbsp;&nbsp;--&nbsp;---------<br />&nbsp;result&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;op&nbsp;test<br />&nbsp;---------&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---------&nbsp;&nbsp;--&nbsp;---------<br /></span><span style="color: #007700">EOH;<br /><br /><br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;Here&nbsp;are&nbsp;the&nbsp;examples.<br />&nbsp;*/<br /><br /></span><span style="color: #0000BB">$values&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$test&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\n&nbsp;Bitwise&nbsp;AND&nbsp;\n"</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$values&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">&amp;&nbsp;</span><span style="color: #0000BB">$test</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&amp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$test</span><span style="color: #007700">);<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\n&nbsp;Bitwise&nbsp;Inclusive&nbsp;OR&nbsp;\n"</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$values&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">$test</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'|'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$test</span><span style="color: #007700">);<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\n&nbsp;Bitwise&nbsp;Exclusive&nbsp;OR&nbsp;(XOR)&nbsp;\n"</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$values&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">^&nbsp;</span><span style="color: #0000BB">$test</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'^'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$test</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>The above example will output:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
 ---------     ---------  -- ---------
 result        value      op test
 ---------     ---------  -- ---------
 Bitwise AND
( 0 = 0000) = ( 0 = 0000) &amp; ( 5 = 0101)
( 1 = 0001) = ( 1 = 0001) &amp; ( 5 = 0101)
( 0 = 0000) = ( 2 = 0010) &amp; ( 5 = 0101)
( 4 = 0100) = ( 4 = 0100) &amp; ( 5 = 0101)
( 0 = 0000) = ( 8 = 1000) &amp; ( 5 = 0101)

 Bitwise Inclusive OR
( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101)
( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101)
( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101)
( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101)
(13 = 1101) = ( 8 = 1000) | ( 5 = 0101)

 Bitwise Exclusive OR (XOR)
( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101)
( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101)
( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101)
( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101)
(13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101)
</pre></div>
     </div>
    </div>
   </p>
   <p class="para">
    <div class="example" id="example-119">
     <p><strong>Example #2 Bitwise XOR operations on strings</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">12&nbsp;</span><span style="color: #007700">^&nbsp;</span><span style="color: #0000BB">9</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Outputs&nbsp;'5'<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"12"&nbsp;</span><span style="color: #007700">^&nbsp;</span><span style="color: #DD0000">"9"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Outputs&nbsp;the&nbsp;Backspace&nbsp;character&nbsp;(ascii&nbsp;8)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;('1'&nbsp;(ascii&nbsp;49))&nbsp;^&nbsp;('9'&nbsp;(ascii&nbsp;57))&nbsp;=&nbsp;#8<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"hallo"&nbsp;</span><span style="color: #007700">^&nbsp;</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Outputs&nbsp;the&nbsp;ascii&nbsp;values&nbsp;#0&nbsp;#4&nbsp;#0&nbsp;#0&nbsp;#0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;'a'&nbsp;^&nbsp;'e'&nbsp;=&nbsp;#4<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">^&nbsp;</span><span style="color: #DD0000">"3"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Outputs&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;2&nbsp;^&nbsp;((int)"3")&nbsp;==&nbsp;1<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"2"&nbsp;</span><span style="color: #007700">^&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Outputs&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;((int)"2")&nbsp;^&nbsp;3&nbsp;==&nbsp;1<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    <div class="example" id="example-120">
     <p><strong>Example #3 Bit shifting on integers</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;Here&nbsp;are&nbsp;the&nbsp;examples.<br />&nbsp;*/<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"\n---&nbsp;BIT&nbsp;SHIFT&nbsp;RIGHT&nbsp;ON&nbsp;POSITIVE&nbsp;INTEGERS&nbsp;---\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&gt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&gt;&gt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'copy&nbsp;of&nbsp;sign&nbsp;bit&nbsp;shifted&nbsp;into&nbsp;left&nbsp;side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&gt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&gt;&gt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&gt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&gt;&gt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bits&nbsp;shift&nbsp;out&nbsp;right&nbsp;side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&gt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&gt;&gt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'same&nbsp;result&nbsp;as&nbsp;above;&nbsp;can&nbsp;not&nbsp;shift&nbsp;beyond&nbsp;0'</span><span style="color: #007700">);<br /><br /><br />echo&nbsp;</span><span style="color: #DD0000">"\n---&nbsp;BIT&nbsp;SHIFT&nbsp;RIGHT&nbsp;ON&nbsp;NEGATIVE&nbsp;INTEGERS&nbsp;---\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&gt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&gt;&gt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'copy&nbsp;of&nbsp;sign&nbsp;bit&nbsp;shifted&nbsp;into&nbsp;left&nbsp;side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&gt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&gt;&gt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bits&nbsp;shift&nbsp;out&nbsp;right&nbsp;side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&gt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&gt;&gt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'same&nbsp;result&nbsp;as&nbsp;above;&nbsp;can&nbsp;not&nbsp;shift&nbsp;beyond&nbsp;-1'</span><span style="color: #007700">);<br /><br /><br />echo&nbsp;</span><span style="color: #DD0000">"\n---&nbsp;BIT&nbsp;SHIFT&nbsp;LEFT&nbsp;ON&nbsp;POSITIVE&nbsp;INTEGERS&nbsp;---\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&lt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;&lt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'zeros&nbsp;fill&nbsp;in&nbsp;right&nbsp;side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&lt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;&lt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&lt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;&lt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'sign&nbsp;bits&nbsp;get&nbsp;shifted&nbsp;out'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&lt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;&lt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bits&nbsp;shift&nbsp;out&nbsp;left&nbsp;side'</span><span style="color: #007700">);<br /><br /><br />echo&nbsp;</span><span style="color: #DD0000">"\n---&nbsp;BIT&nbsp;SHIFT&nbsp;LEFT&nbsp;ON&nbsp;NEGATIVE&nbsp;INTEGERS&nbsp;---\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&lt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;&lt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'zeros&nbsp;fill&nbsp;in&nbsp;right&nbsp;side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&lt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;&lt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&lt;&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;&lt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bits&nbsp;shift&nbsp;out&nbsp;left&nbsp;side,&nbsp;including&nbsp;sign&nbsp;bit'</span><span style="color: #007700">);<br /><br /><br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;Ignore&nbsp;this&nbsp;bottom&nbsp;section,<br />&nbsp;*&nbsp;it&nbsp;is&nbsp;just&nbsp;formatting&nbsp;to&nbsp;make&nbsp;output&nbsp;clearer.<br />&nbsp;*/<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$op</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$note&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'%0'&nbsp;</span><span style="color: #007700">.&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"b\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Expression:&nbsp;%d&nbsp;=&nbsp;%d&nbsp;%s&nbsp;%d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$op</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$places</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;Decimal:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"&nbsp;&nbsp;val=%d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"&nbsp;&nbsp;res=%d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;Binary:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">'&nbsp;&nbsp;val='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">'&nbsp;&nbsp;res='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$note</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;NOTE:&nbsp;</span><span style="color: #0000BB">$note</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>Output of the above example on 32 bit machines:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>

--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---
Expression: 2 = 4 &gt;&gt; 1
 Decimal:
  val=4
  res=2
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000010
 NOTE: copy of sign bit shifted into left side

Expression: 1 = 4 &gt;&gt; 2
 Decimal:
  val=4
  res=1
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000001

Expression: 0 = 4 &gt;&gt; 3
 Decimal:
  val=4
  res=0
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000000
 NOTE: bits shift out right side

Expression: 0 = 4 &gt;&gt; 4
 Decimal:
  val=4
  res=0
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000000
 NOTE: same result as above; can not shift beyond 0


--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
Expression: -2 = -4 &gt;&gt; 1
 Decimal:
  val=-4
  res=-2
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111110
 NOTE: copy of sign bit shifted into left side

Expression: -1 = -4 &gt;&gt; 2
 Decimal:
  val=-4
  res=-1
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111111
 NOTE: bits shift out right side

Expression: -1 = -4 &gt;&gt; 3
 Decimal:
  val=-4
  res=-1
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111111
 NOTE: same result as above; can not shift beyond -1


--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
Expression: 8 = 4 &lt;&lt; 1
 Decimal:
  val=4
  res=8
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000001000
 NOTE: zeros fill in right side

Expression: 1073741824 = 4 &lt;&lt; 28
 Decimal:
  val=4
  res=1073741824
 Binary:
  val=00000000000000000000000000000100
  res=01000000000000000000000000000000

Expression: -2147483648 = 4 &lt;&lt; 29
 Decimal:
  val=4
  res=-2147483648
 Binary:
  val=00000000000000000000000000000100
  res=10000000000000000000000000000000
 NOTE: sign bits get shifted out

Expression: 0 = 4 &lt;&lt; 30
 Decimal:
  val=4
  res=0
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000000
 NOTE: bits shift out left side


--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---
Expression: -8 = -4 &lt;&lt; 1
 Decimal:
  val=-4
  res=-8
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111000
 NOTE: zeros fill in right side

Expression: -2147483648 = -4 &lt;&lt; 29
 Decimal:
  val=-4
  res=-2147483648
 Binary:
  val=11111111111111111111111111111100
  res=10000000000000000000000000000000

Expression: 0 = -4 &lt;&lt; 30
 Decimal:
  val=-4
  res=0
 Binary:
  val=11111111111111111111111111111100
  res=00000000000000000000000000000000
 NOTE: bits shift out left side, including sign bit
</pre></div>
     </div>
     <div class="example-contents"><p>Output of the above example on 64 bit machines:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>

--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---
Expression: 2 = 4 &gt;&gt; 1
 Decimal:
  val=4
  res=2
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000010
 NOTE: copy of sign bit shifted into left side

Expression: 1 = 4 &gt;&gt; 2
 Decimal:
  val=4
  res=1
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000001

Expression: 0 = 4 &gt;&gt; 3
 Decimal:
  val=4
  res=0
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000000
 NOTE: bits shift out right side

Expression: 0 = 4 &gt;&gt; 4
 Decimal:
  val=4
  res=0
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000000
 NOTE: same result as above; can not shift beyond 0


--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
Expression: -2 = -4 &gt;&gt; 1
 Decimal:
  val=-4
  res=-2
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111110
 NOTE: copy of sign bit shifted into left side

Expression: -1 = -4 &gt;&gt; 2
 Decimal:
  val=-4
  res=-1
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111111
 NOTE: bits shift out right side

Expression: -1 = -4 &gt;&gt; 3
 Decimal:
  val=-4
  res=-1
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111111
 NOTE: same result as above; can not shift beyond -1


--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
Expression: 8 = 4 &lt;&lt; 1
 Decimal:
  val=4
  res=8
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000001000
 NOTE: zeros fill in right side

Expression: 4611686018427387904 = 4 &lt;&lt; 60
 Decimal:
  val=4
  res=4611686018427387904
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0100000000000000000000000000000000000000000000000000000000000000

Expression: -9223372036854775808 = 4 &lt;&lt; 61
 Decimal:
  val=4
  res=-9223372036854775808
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=1000000000000000000000000000000000000000000000000000000000000000
 NOTE: sign bits get shifted out

Expression: 0 = 4 &lt;&lt; 62
 Decimal:
  val=4
  res=0
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000000
 NOTE: bits shift out left side


--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---
Expression: -8 = -4 &lt;&lt; 1
 Decimal:
  val=-4
  res=-8
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111000
 NOTE: zeros fill in right side

Expression: -9223372036854775808 = -4 &lt;&lt; 61
 Decimal:
  val=-4
  res=-9223372036854775808
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1000000000000000000000000000000000000000000000000000000000000000

Expression: 0 = -4 &lt;&lt; 62
 Decimal:
  val=-4
  res=0
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=0000000000000000000000000000000000000000000000000000000000000000
 NOTE: bits shift out left side, including sign bit
</pre></div>
     </div>
    </div>
   </p>
   <div class="warning"><strong class="warning">Warning</strong>
    <p class="para">
     Don&#039;t right shift for more than 32 bits on 32 bits systems.
     Don&#039;t left shift in case it results to number longer than 32 bits.
     Use functions from the gmp extension for bitwise manipulation on
     numbers beyond PHP_INT_MAX.
    </p>
   </div>
   <p class="para">
    See also
     <span class="function"><a href="function.pack.html" class="function">pack()</a></span>,
     <span class="function"><a href="function.unpack.html" class="function">unpack()</a></span>,
     <span class="function"><a href="function.gmp-and.html" class="function">gmp_and()</a></span>,
     <span class="function"><a href="function.gmp-or.html" class="function">gmp_or()</a></span>,
     <span class="function"><a href="function.gmp-xor.html" class="function">gmp_xor()</a></span>,
     <span class="function"><a href="function.gmp-testbit.html" class="function">gmp_testbit()</a></span>,
     <span class="function"><a href="function.gmp-clrbit.html" class="function">gmp_clrbit()</a></span>
   </p>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.assignment.html">Assignment Operators</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.comparison.html">Comparison Operators</a></div>
 <div class="up"><a href="language.operators.html">Operators</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>