cvs: phpdoc /en/language operators.xml

From: Date: Fri, 03 Apr 2009 00:24:24 +0000
Subject: cvs: phpdoc /en/language operators.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-3648@lists.php.net to get a copy of this message
danielc Fri Apr 3 00:24:24 2009 UTC Modified files: /phpdoc/en/language operators.xml Log: * Bitwise: provide examples for &, | and ^ on integers. http://cvs.php.net/viewvc.cgi/phpdoc/en/language/operators.xml?r1=1.132&r2=1.133&diff_format=u Index: phpdoc/en/language/operators.xml diff -u phpdoc/en/language/operators.xml:1.132 phpdoc/en/language/operators.xml:1.133 --- phpdoc/en/language/operators.xml:1.132 Thu Apr 2 23:31:53 2009 +++ phpdoc/en/language/operators.xml Fri Apr 3 00:24:23 2009 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.132 $ --> +<!-- $Revision: 1.133 $ --> <chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook"> <title>Operators</title> <simpara> @@ -436,6 +436,84 @@ </table> <para> <example> + <title>Bitwise operations on integers</title> + <programlisting role="php"> +<![CDATA[ +<?php +/* + * Ignore the top section, + * it is just formatting to make output clearer. + */ + +$format = '(%1$2d = %1$04b) = (%2$2d = %2$04b)' + . ' %3$s (%4$2d = %4$04b)' . "\n"; + +echo <<<EOH + --------- --------- -- --------- + result value op test + --------- --------- -- --------- +EOH; + + +/* + * Here are the examples. + */ + +$values = array(0, 1, 2, 4, 8); +$test = 1 + 4; + +echo "\n Bitwise AND \n"; +foreach ($values as $value) { + $result = $value & $test; + printf($format, $result, $value, '&', $test); +} + +echo "\n Bitwise Inclusive OR \n"; +foreach ($values as $value) { + $result = $value | $test; + printf($format, $result, $value, '|', $test); +} + +echo "\n Bitwise Exclusive OR (XOR) \n"; +foreach ($values as $value) { + $result = $value ^ $test; + printf($format, $result, $value, '^', $test); +} +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ + --------- --------- -- --------- + 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) +]]> + </screen> + </example> + </para> + <para> + <example> <title>Bitwise operations on strings</title> <programlisting role="php"> <![CDATA[

« previous php.doc.cvs (#3648) next »