Improved information on behaviour of bitwise operators on string operands

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@333319 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Chris Wright 2014-04-15 09:46:11 +00:00
parent 7f70616b6a
commit 7e378e46ef

View file

@ -611,9 +611,22 @@ $o = &new C;
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.
If both operands for the <literal>&amp;</literal>, <literal>|</literal> and
<literal>^</literal> operators are strings, then the operation will be
performed on the ASCII values of the characters that make up the strings and
the result will be a string. In all other cases, both operands will be
<link linkend="language.types.integer.casting">converted to integers</link>
and the result will be an integer.
</para>
<para>
If the operand for the <literal>~</literal> operator is a string, the
operation will be performed on the ASCII values of the characters that make
up the string and the result will be a string, otherwise the operand and the
result will be treated as integers.
</para>
<para>
Both operands and the result for the <literal>&lt;&lt;</literal> and
<literal>&gt;&gt;</literal> operators are always treated as integers.
</para>
<para>
<informalexample>
@ -1165,10 +1178,14 @@ Expression: 0 = -4 << 62
</para>
<warning>
<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.
Shifting integers by values greater than or equal to the system long
integer width results in undefined behavior. In other words, don't shift
more than 31 bits on a 32-bit system, and don't shift more than 63 bits on
a 64-bit system.
</para>
<para>
Use functions from the <link linkend="book.gmp">gmp</link> extension for
bitwise manipulation on numbers beyond <literal>PHP_INT_MAX</literal>.
</para>
</warning>
<para>