Bug #68600 [Opn->Nab]: shift
| From: | requinix@php.net | Date: | Sat, 13 Dec 2014 08:31:53 +0000 |
| Subject: | Bug #68600 [Opn->Nab]: shift | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189048@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68600&edit=1
ID: 68600
Updated by: requinix@php.net
Reported by: armenhak59 at yahoo dot com
Summary: shift
-Status: Open
+Status: Not a bug
Type: Bug
-Package: Compile Failure
+Package: *General Issues
Operating System: windows 7 32 bit
PHP Version: 5.5.19
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
http://php.net/manual/en/language.operators.bitwise.php
"Warning
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."
This will change in PHP 7.
https://wiki.php.net/rfc/integer_semantics
"3. Left bitwise shifts by a number of bits beyond the bit width of an integer will always
result in 0, even on CPUs which wrap around"
Previous Comments:
------------------------------------------------------------------------
[2014-12-13 07:41:54] armenhak59 at yahoo dot com
Description:
------------
<?php
echo "SHIFT LEFT DO NOT WORKS RIGHT FOR NEGATIV NUMBER -1, BUT WORKS FOR OTHER
NUMBERS<br><br>";
$format = '%0' . 32 . "b<br>";
$val = -1;
echo "<br> WRONG FOR -1<br>";
printf(" val = %d String: %s = %d %s %d<br>", $val, 'res', $val,
'<<', 32);
$val = $val << 32;
printf(' val = ' . $format, $val);
echo "<br> IT MUST BE <br>";
printf('..... = ' . $format, 0);
echo "<br><br><br>FOR OTHER NUMBERS IT WORKS CORRECT:";
printf("<br><br> val = %d String: %s = %d %s %d<br>", $val,
'res', $val, '<<', 1);
$val = $val << 1;
printf(' val = ' . $format, $val);
printf("<br><br> val = %d String: %s = %d %s %d<br>", $val,
'res', $val, '<<', 31);
$val = $val << 31;
printf(' val = ' . $format, $val);
?>
Test script:
---------------
<?php
echo "SHIFT LEFT DO NOT WORKS RIGHT FOR NEGATIV NUMBER -1, BUT WORKS FOR OTHER
NUMBERS<br><br>";
$format = '%0' . 32 . "b<br>";
$val = -1;
echo "<br> WRONG FOR -1<br>";
printf(" val = %d String: %s = %d %s %d<br>", $val, 'res', $val,
'<<', 32);
$val = $val << 32;
printf(' val = ' . $format, $val);
echo "<br> IT MUST BE <br>";
printf('..... = ' . $format, 0);
echo "<br><br><br>FOR OTHER NUMBERS IT WORKS CORRECT:";
printf("<br><br> val = %d String: %s = %d %s %d<br>", $val,
'res', $val, '<<', 1);
$val = $val << 1;
printf(' val = ' . $format, $val);
printf("<br><br> val = %d String: %s = %d %s %d<br>", $val,
'res', $val, '<<', 31);
$val = $val << 31;
printf(' val = ' . $format, $val);
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68600&edit=1