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

  • SEARCH
  • Home
  • 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 7617787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:14:04+00:00 2026-05-31T03:14:04+00:00

Got some weird behavior I was wondering if someone could clear up for me.

  • 0

Got some weird behavior I was wondering if someone could clear up for me.

Check it out

$hex = 0x80008000;

print_r(decbin(intval($hex)) . '<br/>');
print_r(decbin($hex));

Outputs

10000000000000001000000000000000
10000000000000001000000000000000

As expected.

But

$hex = 0x80008000;

print_r(decbin(~intval($hex)) . '<br/>');
print_r(decbin(~$hex));

Outputs

1111111111111110111111111111111
1111111111111111111111111111111

Why is the middle bit not switching when $hex is negated?

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-31T03:14:05+00:00Added an answer on May 31, 2026 at 3:14 am

    Gonna give a shot to my own question here.

    Yes this is a 32-bit / 64-bit difference.

    In 32-bit systems, a float type has to take up two memory spaces to get the required 64 bits. Php uses double-precision (see http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers)

    The $hex evaluates to a float type. Intval and decbin functions convert this into an int type (1st example above)

    In the 2nd example we are using the not bitwise operator BEFORE we use decbin. This flips the bits in the two-memory space double-precision float first, and then is converted to int second. Giving us something different than what we expected.

    Indeed, if we put the negate inside of the intval() like so:

    $hex = 0x80008000;
    
    print_r(decbin(intval(~$hex)) . '<br/>');
    print_r(decbin(~$hex));
    

    We get

    1111111111111111111111111111111
    1111111111111111111111111111111
    

    As output.

    I’m not good enough to prove this with math yet (which can be figured out with the help of this article http://en.wikipedia.org/wiki/Double_precision). But maybe when I have time later -_-

    I think it’s very important to learn how numbers are represented in computers so we can understand anomalies like this and not call them bugs.

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

Sidebar

Related Questions

I've got some very weird behavior in my PHP code. I don't know if
Alright, I have some weird behavior and this question goes to the people out
I've got some weird behavior and I can only assume is because of the
I've got a bug caused by some weird behavior. I might be fundamentally missing
I have a Rails 3.1 app where I just got some weird behavior. I
I got some kind of weird behavior while trying to use all the power
I got some weird error with response.redirect() and the project wasn't building at all..
i've got some weird characters in my database, which seem to mess up django
got a weird question for you based on some weird happenings on our site.
I got some legacy code with that caption spread out as comment almost in

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.