1

How to make php code "echo 62837 << -16;" return same result between windows and linux?

windows result:-176881664

linux result:-759700962142060544

1 Answers1

2

Shifting by a negative number throws an exception in PHP 7+ so it's obviously invalid to do so.

Fatal error: Uncaught ArithmeticError: Bit shift by negative number

In C, it invokes undefined behavior, see: Left shifting with a negative shift count

To answer your question: You can't make code that invokes undefined behavior return consistent results across platforms. That code needs to be modified to not shift by a negative number.

Community
  • 1
  • 1
Shira
  • 6,392
  • 2
  • 25
  • 27
  • I got the code from http://www.tero.co.uk/des/code.php. A DES Source Code. – user6498456 Jun 22 '16 at 10:04
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/12772293) – Jonathan Argentiero Jun 22 '16 at 13:06
  • @JonathanArgentiero I believe you've incorrectly flagged this question. – ceejayoz Jun 22 '16 at 13:39
  • 1
    @JonathanArgentiero I reworded the answer, however if the OP asks how to make *invalid code* work then I consider telling them *the code is invalid* a valid answer. I will not rewrite some (originally not mentioned) DES encryption functions for them. – Shira Jun 22 '16 at 13:40