Q:

Negation of Hex in PHP, funny behavior

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?

A:

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.

