Pikachu2000 Posted January 7, 2012 Share Posted January 7, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254568-bitwise-~-not-wip2long-32-bit-vs-64-bit-os/ Share on other sites More sharing options...
gizmola Posted January 7, 2012 Share Posted January 7, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/254568-bitwise-~-not-wip2long-32-bit-vs-64-bit-os/#findComment-1305389 Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2012 Author Share Posted January 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254568-bitwise-~-not-wip2long-32-bit-vs-64-bit-os/#findComment-1305406 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.