mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
* Bitwise: provide examples of turning bits on and off.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@278664 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
4bc37ab2c6
commit
2b04b69cc0
1 changed files with 56 additions and 4 deletions
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.140 $ -->
|
||||
<!-- $Revision: 1.141 $ -->
|
||||
<chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Operators</title>
|
||||
<simpara>
|
||||
|
@ -441,7 +441,6 @@ $b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
|
|||
Right shifts have copies of the sign bit shifted in on the left,
|
||||
meaning the sign of an operand is preserved.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Use parentheses to ensure the desired
|
||||
<link linkend="language.operators.precedence">precedence</link>.
|
||||
|
@ -450,13 +449,66 @@ $b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
|
|||
<userinput>($a & $b) == true</userinput> evaluates the bitwise and
|
||||
then the equivalency.
|
||||
</para>
|
||||
|
||||
<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.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
<informalexample>
|
||||
<para>
|
||||
<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:
|
||||
<userinput>E_ALL & ~E_NOTICE</userinput>
|
||||
</literallayout>
|
||||
</para>
|
||||
<para>
|
||||
<literallayout>
|
||||
This works by starting with E_ALL:
|
||||
<computeroutput>00000000000000000111011111111111</computeroutput>
|
||||
Then taking the value of E_NOTICE...
|
||||
<computeroutput>00000000000000000000000000001000</computeroutput>
|
||||
... and inverting it via <userinput>~</userinput>:
|
||||
<computeroutput>11111111111111111111111111110111</computeroutput>
|
||||
Finally, it uses AND (&) to find the bits turned
|
||||
on in both values:
|
||||
<computeroutput>00000000000000000111011111110111</computeroutput>
|
||||
</literallayout>
|
||||
</para>
|
||||
<para>
|
||||
<literallayout>
|
||||
Another way to accomplish that is using XOR (<userinput>^</userinput>)
|
||||
to find bits that are on in only one value or the other:
|
||||
<userinput>E_ALL ^ E_NOTICE</userinput>
|
||||
</literallayout>
|
||||
</para>
|
||||
</informalexample>
|
||||
</para>
|
||||
<para>
|
||||
<informalexample>
|
||||
<para>
|
||||
<literallayout>
|
||||
error_reporting can also be used to demonstrate turning bits on.
|
||||
The way to show just errors and recoverable errors is:
|
||||
<userinput>E_ERROR | E_RECOVERABLE_ERROR</userinput>
|
||||
</literallayout>
|
||||
</para>
|
||||
<para>
|
||||
<literallayout>
|
||||
This process combines E_ERROR
|
||||
<computeroutput>00000000000000000000000000000001</computeroutput>
|
||||
and
|
||||
<computeroutput>00000000000000000001000000000000</computeroutput>
|
||||
using the OR (<userinput>|</userinput>) operator
|
||||
to get the bits turned on in either value:
|
||||
<computeroutput>00000000000000000001000000000001</computeroutput>
|
||||
</literallayout>
|
||||
</para>
|
||||
</informalexample>
|
||||
</para>
|
||||
<para>
|
||||
<example>
|
||||
<title>Bitwise AND, OR and XOR operations on integers</title>
|
||||
|
|
Loading…
Reference in a new issue