cvs: phpdoc /en/language operators.xml
| From: | Daniel Convissor | Date: | Mon, 13 Apr 2009 16:04:20 +0000 |
| Subject: | cvs: phpdoc /en/language operators.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-3705@lists.php.net to get a copy of this message | ||
danielc Mon Apr 13 16:04:20 2009 UTC
Modified files:
/phpdoc/en/language operators.xml
Log:
* Bitwise: provide examples of turning bits on and off.
http://cvs.php.net/viewvc.cgi/phpdoc/en/language/operators.xml?r1=1.140&r2=1.141&diff_format=u
Index: phpdoc/en/language/operators.xml
diff -u phpdoc/en/language/operators.xml:1.140 phpdoc/en/language/operators.xml:1.141
--- phpdoc/en/language/operators.xml:1.140 Mon Apr 13 05:40:33 2009
+++ phpdoc/en/language/operators.xml Mon Apr 13 16:04:20 2009
@@ -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 @@
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 @@
<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>