php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68600 shift
Submitted: 2014-12-13 07:41 UTC Modified: 2014-12-13 08:31 UTC
From: armenhak59 at yahoo dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.5.19 OS: windows 7 32 bit
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: armenhak59 at yahoo dot com
New email:
PHP Version: OS:

 

 [2014-12-13 07:41 UTC] 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);
?>



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-13 08:31 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Package: Compile Failure +Package: *General Issues
 [2014-12-13 08:31 UTC] requinix@php.net
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"
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 02:00:01 2026 UTC