Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 90385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:51:54+00:00 2026-05-10T22:51:54+00:00

We have some integer arithmetic which for historical reasons has to work the same

  • 0

We have some integer arithmetic which for historical reasons has to work the same on PHP as it does in a few statically typed languages. Since we last upgraded PHP the behavior for overflowing integers has changed. Basically we are using following formula:

function f($x1, $x2, $x3, $x4) {    return (($x1 + $x2) ^ $x3) + $x4; } 

However, even with conversions:

function f($x1, $x2, $x3, $x4) {    return intval(intval(intval($x1 + $x2) ^ $x3) + $x4); } 

I am still ending up with the completely wrong number…

For example, with $x1 = -1580033017, $x2 = -2072974554, $x3 = -1170476976) and $x4 = -1007518822, I end up with -30512150 in PHP and 1617621783 in C#.

Just adding together $x1 and $x2 I cannot get the right answer:

In C# I get

(-1580033017 + -2072974554) = 641959725 

In PHP:

intval(intval(-1580033017) + intval(-2072974554)) = -2147483648 

which is the same as:

intval(-1580033017 + -2072974554) = -2147483648 

I don’t mind writing a ‘IntegerOverflowAdd’ function or something, but I can’t quite figure out how (-1580033017 + -2072974554) equals 641959725. (I do recognize that it is -2147483648 + (2 * 2^31), but -2147483648 + 2^31 is -1505523923 which is greater than Int.Min so why is do you add 2*2^31 and not 2^31?)

Any help would be appreciated…

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:51:54+00:00Added an answer on May 10, 2026 at 10:51 pm

    So I solved the problem, and discovered a lot about PHP (at least in the way it handles Integer overflow).

    1) It completely depended on a cross between which platform the machine was running on, which version of PHP, whether or not it had Suhosin Hardened PHP running, and how many bits it was compiled for (32 or 64). 6 machines behaved the way I expected (which was actually wrong, at least wrong according to their documentation) and 3 machines behaved in a way I still can’t explain, and 3 machines behaved according to what the intval command says it does in the documentation.

    2) Intval is supposed to return PHP_INT_MAX when int > PHP_INT_MAX (not int & 0xffffffff), but this only happens on some versions of PHP4 and PHP5. Different versions of PHP return different values when int > PHP_INT_MAX.

    3) The following code can return 3 different results (see 1):

    <?php echo 'Php max int: '.PHP_INT_MAX.'\n'; echo 'The Val: '.(-1580033017 + -2072974554).'\n'; echo 'Intval of the val: '.intval(-3653007571).'\n'; echo 'And 0xffffffff of the val: '.(-3653007571 & 0xffffffff).'\n'; ?> 

    It can return (which appears to be right for Intval but wrong for & 0xffffff)

    Php max int: 2147483647 The Val: -3653007571 Intval of the val: -2147483648 And of the val: -2147483648 

    And it can return (which contradicts the PHP documentation for intval):

    Php max int: 2147483647 The Val: -3653007571 Intval of the val: -641959725 And of the val: -641959725 

    And on 64 Bit machines it returns (which is correct):

    Php max int: 2147483647 The Val: -3653007571 Intval of the val: -3653007571 And of the val: -641959725 

    Solution

    Anyhow, I needed a solution that would work on all these platforms, and not be dependent upon quirks of a particular version of PHP compiled with a particular Max int. Thus I cam up with the following cross-PHP thirtyTwoBitIntval function:

    function thirtyTwoBitIntval($value) {     if ($value < -2147483648)     {         return -(-($value) & 0xffffffff);     }     elseif ($value > 2147483647)     {         return ($value & 0xffffffff);     }     return $value; } 

    Comment

    I do think the designers of PHP should have said an Int is a 32 Bit Int no matter whether it is running on a 32 or 64 or 128 bit machine (like the DotNet CLR for example), and didn’t randomly upconvert it to a float depending on the number of Bits that PHP is compiler under.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your loss of precision comes from the fact that NSNumberFormatter… May 11, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer As you mentioned, the catch-all parameter can only appear at… May 11, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer I didn't test extensively, but this seems to work: (I… May 11, 2026 at 10:49 pm

Related Questions

Say you're writing a C++ application doing lots of floating point arithmetic. Say this
We have two columns in a database which is currently of type varchar(16). Thing
We have our own glue-layer-code-thingamajig that allows us to host the .NET runtime in
We're running a fairly complex app as a portlet on Websphere Portal Server 5.1

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.