Jump to content

Bitwise ~ NOT w/ip2long() - 32 bit vs. 64 bit OS?


Pikachu2000

Recommended Posts

I'm at kind of a loss with this. I've read the docs for ~ bitwise NOT, and for ip2long() and I can't see why this is happening. On 2 machines with 32 bit OSs (one PPC Mac, and one Linux box), this does what I'd expect it to do. On an Intel Mac with a 64 bit version of OS X, it does not.

 

echo abs(~ip2long('255.255.255.0')) + 1;

 

I would expect this to return a value of 256.

 

On a PowerMac, dual G5 OS X 10.5.8, (32 bit) and PHP 5.2.11  it returns 256

On a hosting server (unsure what processors) Linux OS (32 bit) and PHP 5.2.14, it returns 256

On an Intel Mac, Core2 Duo, OS X 10.6.8 (64 bit) and PHP 5.2.17, it returns 4294967042

 

Have I overlooked something in the manual that would explain this, or am I doing something fundamentally wrong with one of the functions?

Link to comment
Share on other sites

Yeah, on 64 bit machines the integer type is going to be larger than 32 bits, so your not is flipping bits above 32.

 

Try:

 

echo PHP_INT_MAX;

 

On both architectures, and I think you'll see the problem.

 

I think this will work for 32 and 64bit, but check.

 

echo (abs(ip2long('255.255.255.0') ^ 4294967295)) + 1;

Link to comment
Share on other sites

Thanks, that triggered the "light bulb moment". I had looked at the PHP_INT_MAX values, and that didn't even dawn on me. The bits from 33 to 64 are OFF after the ip2long(), so the NOT operation reverses them to ON. In my mind, the high order bits shouldn't have even existed after the ip2long. That explains it perfectly well though, thanks gizmola.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.