<!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 & $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 << $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 "multiply by two") </td> </tr> <tr> <td><strong class="userinput"><code>$a >> $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 "divide by two") </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 & $b == true</em> evaluates the equivalency then the bitwise and; while <em>($a & $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' ASCII values. </p> <p class="para"> <div class="informalexample"> <p class="para"> <pre class="literallayout"> PHP'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 & ~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 (&) 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"><?php<br /></span><span style="color: #FF8000">/*<br /> * Ignore the top section,<br /> * it is just formatting to make output clearer.<br /> */<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'(%1$2d = %1$04b) = (%2$2d = %2$04b)'<br /> </span><span style="color: #007700">. </span><span style="color: #DD0000">' %3$s (%4$2d = %4$04b)' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo <<<EOH<br /></span><span style="color: #DD0000"> --------- --------- -- ---------<br /> result value op test<br /> --------- --------- -- ---------<br /></span><span style="color: #007700">EOH;<br /><br /><br /></span><span style="color: #FF8000">/*<br /> * Here are the examples.<br /> */<br /><br /></span><span style="color: #0000BB">$values </span><span style="color: #007700">= array(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$test </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"\n Bitwise AND \n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$values </span><span style="color: #007700">as </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$value </span><span style="color: #007700">& </span><span style="color: #0000BB">$test</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$result</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #DD0000">'&'</span><span style="color: #007700">, </span><span style="color: #0000BB">$test</span><span style="color: #007700">);<br />}<br /><br />echo </span><span style="color: #DD0000">"\n Bitwise Inclusive OR \n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$values </span><span style="color: #007700">as </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$value </span><span style="color: #007700">| </span><span style="color: #0000BB">$test</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$result</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #DD0000">'|'</span><span style="color: #007700">, </span><span style="color: #0000BB">$test</span><span style="color: #007700">);<br />}<br /><br />echo </span><span style="color: #DD0000">"\n Bitwise Exclusive OR (XOR) \n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$values </span><span style="color: #007700">as </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$value </span><span style="color: #007700">^ </span><span style="color: #0000BB">$test</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$result</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #DD0000">'^'</span><span style="color: #007700">, </span><span style="color: #0000BB">$test</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></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) & ( 5 = 0101) ( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101) ( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101) ( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101) ( 0 = 0000) = ( 8 = 1000) & ( 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"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">12 </span><span style="color: #007700">^ </span><span style="color: #0000BB">9</span><span style="color: #007700">; </span><span style="color: #FF8000">// Outputs '5'<br /><br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"12" </span><span style="color: #007700">^ </span><span style="color: #DD0000">"9"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Outputs the Backspace character (ascii 8)<br /> // ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8<br /><br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"hallo" </span><span style="color: #007700">^ </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Outputs the ascii values #0 #4 #0 #0 #0<br /> // 'a' ^ 'e' = #4<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">2 </span><span style="color: #007700">^ </span><span style="color: #DD0000">"3"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Outputs 1<br /> // 2 ^ ((int)"3") == 1<br /><br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"2" </span><span style="color: #007700">^ </span><span style="color: #0000BB">3</span><span style="color: #007700">; </span><span style="color: #FF8000">// Outputs 1<br /> // ((int)"2") ^ 3 == 1<br /></span><span style="color: #0000BB">?></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"><?php<br /></span><span style="color: #FF8000">/*<br /> * Here are the examples.<br /> */<br /><br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"\n--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700">>> </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'>>'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'copy of sign bit shifted into left side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700">>> </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'>>'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700">>> </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'>>'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'bits shift out right side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700">>> </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'>>'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'same result as above; can not shift beyond 0'</span><span style="color: #007700">);<br /><br /><br />echo </span><span style="color: #DD0000">"\n--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= -</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700">>> </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'>>'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'copy of sign bit shifted into left side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= -</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700">>> </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'>>'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'bits shift out right side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= -</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700">>> </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'>>'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'same result as above; can not shift beyond -1'</span><span style="color: #007700">);<br /><br /><br />echo </span><span style="color: #DD0000">"\n--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700"><< </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'<<'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'zeros fill in right side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= (</span><span style="color: #0000BB">PHP_INT_SIZE </span><span style="color: #007700">* </span><span style="color: #0000BB">8</span><span style="color: #007700">) - </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700"><< </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'<<'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= (</span><span style="color: #0000BB">PHP_INT_SIZE </span><span style="color: #007700">* </span><span style="color: #0000BB">8</span><span style="color: #007700">) - </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700"><< </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'<<'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'sign bits get shifted out'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= (</span><span style="color: #0000BB">PHP_INT_SIZE </span><span style="color: #007700">* </span><span style="color: #0000BB">8</span><span style="color: #007700">) - </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700"><< </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'<<'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'bits shift out left side'</span><span style="color: #007700">);<br /><br /><br />echo </span><span style="color: #DD0000">"\n--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= -</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700"><< </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'<<'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'zeros fill in right side'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= -</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= (</span><span style="color: #0000BB">PHP_INT_SIZE </span><span style="color: #007700">* </span><span style="color: #0000BB">8</span><span style="color: #007700">) - </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700"><< </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'<<'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= -</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$places </span><span style="color: #007700">= (</span><span style="color: #0000BB">PHP_INT_SIZE </span><span style="color: #007700">* </span><span style="color: #0000BB">8</span><span style="color: #007700">) - </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$val </span><span style="color: #007700"><< </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">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #DD0000">'<<'</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #DD0000">'bits shift out left side, including sign bit'</span><span style="color: #007700">);<br /><br /><br /></span><span style="color: #FF8000">/*<br /> * Ignore this bottom section,<br /> * it is just formatting to make output clearer.<br /> */<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #0000BB">$op</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">, </span><span style="color: #0000BB">$note </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'%0' </span><span style="color: #007700">. (</span><span style="color: #0000BB">PHP_INT_SIZE </span><span style="color: #007700">* </span><span style="color: #0000BB">8</span><span style="color: #007700">) . </span><span style="color: #DD0000">"b\n"</span><span style="color: #007700">;<br /><br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Expression: %d = %d %s %d\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$res</span><span style="color: #007700">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">, </span><span style="color: #0000BB">$op</span><span style="color: #007700">, </span><span style="color: #0000BB">$places</span><span style="color: #007700">);<br /><br /> echo </span><span style="color: #DD0000">" Decimal:\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">" val=%d\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">" res=%d\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /><br /> echo </span><span style="color: #DD0000">" Binary:\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">' val=' </span><span style="color: #007700">. </span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$val</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">' res=' </span><span style="color: #007700">. </span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /><br /> if (</span><span style="color: #0000BB">$note</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">" NOTE: </span><span style="color: #0000BB">$note</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> }<br /><br /> echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></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 >> 1 Decimal: val=4 res=2 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000010 NOTE: copy of sign bit shifted into left side Expression: 1 = 4 >> 2 Decimal: val=4 res=1 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000001 Expression: 0 = 4 >> 3 Decimal: val=4 res=0 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000000 NOTE: bits shift out right side Expression: 0 = 4 >> 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 >> 1 Decimal: val=-4 res=-2 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111110 NOTE: copy of sign bit shifted into left side Expression: -1 = -4 >> 2 Decimal: val=-4 res=-1 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111111 NOTE: bits shift out right side Expression: -1 = -4 >> 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 << 1 Decimal: val=4 res=8 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000001000 NOTE: zeros fill in right side Expression: 1073741824 = 4 << 28 Decimal: val=4 res=1073741824 Binary: val=00000000000000000000000000000100 res=01000000000000000000000000000000 Expression: -2147483648 = 4 << 29 Decimal: val=4 res=-2147483648 Binary: val=00000000000000000000000000000100 res=10000000000000000000000000000000 NOTE: sign bits get shifted out Expression: 0 = 4 << 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 << 1 Decimal: val=-4 res=-8 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111000 NOTE: zeros fill in right side Expression: -2147483648 = -4 << 29 Decimal: val=-4 res=-2147483648 Binary: val=11111111111111111111111111111100 res=10000000000000000000000000000000 Expression: 0 = -4 << 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 >> 1 Decimal: val=4 res=2 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000010 NOTE: copy of sign bit shifted into left side Expression: 1 = 4 >> 2 Decimal: val=4 res=1 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000001 Expression: 0 = 4 >> 3 Decimal: val=4 res=0 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out right side Expression: 0 = 4 >> 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 >> 1 Decimal: val=-4 res=-2 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111110 NOTE: copy of sign bit shifted into left side Expression: -1 = -4 >> 2 Decimal: val=-4 res=-1 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111111 NOTE: bits shift out right side Expression: -1 = -4 >> 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 << 1 Decimal: val=4 res=8 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000001000 NOTE: zeros fill in right side Expression: 4611686018427387904 = 4 << 60 Decimal: val=4 res=4611686018427387904 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0100000000000000000000000000000000000000000000000000000000000000 Expression: -9223372036854775808 = 4 << 61 Decimal: val=4 res=-9223372036854775808 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=1000000000000000000000000000000000000000000000000000000000000000 NOTE: sign bits get shifted out Expression: 0 = 4 << 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 << 1 Decimal: val=-4 res=-8 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111000 NOTE: zeros fill in right side Expression: -9223372036854775808 = -4 << 61 Decimal: val=-4 res=-9223372036854775808 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1000000000000000000000000000000000000000000000000000000000000000 Expression: 0 = -4 << 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't right shift for more than 32 bits on 32 bits systems. Don'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>