Re: Bitwise NOT operator?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



  Thanks to everyone who responded.  I've dealt with binary math
before, but it never occurred to me (and doesn't seem to be anywhere
in the document page at php.net!) that it would automatically pad the
number I entered.

  The example I gave was essentially a test I was running:  in the
real script, I'm trying to calculate the number of IP addresses in a
given subnet, and I was getting some seriously weird results.  I'm
still not entirely sure what's going on, but this should help.

Thanks again!
-Alex


On Fri, Aug 20, 2010 at 11:42 AM, Peter Lind <peter.e.lind@xxxxxxxxx> wrote:
> On 20 August 2010 17:41, Peter Lind <peter.e.lind@xxxxxxxxx> wrote:
>> On 20 August 2010 17:10, Andy McKenzie <amckenzie4@xxxxxxxxx> wrote:
>>> Hey everyone,
>>>
>>>  I'm really not sure what's going on here:  basically, the bitwise
>>> NOT operator seems to simply not work.  Here's an example of what I
>>> see.
>>>
>>> ============Script============
>>>
>>> $ cat bintest2.php
>>>
>>> <?php
>>>
>>> $bin = 2;
>>> $notbin = ~$bin;
>>>
>>> echo "Bin: " . decbin($bin) . "  !bin:  " . decbin($notbin) . "\n";
>>> echo "Bin: $bin  !bin:  $notbin\n";
>>>
>>>
>>> ?>
>>> =============================
>>>
>>>
>>> ============Output============
>>>
>>> $ php bintest2.php
>>> Bin: 10  !bin:  11111111111111111111111111111101
>>> Bin: 2  !bin:  -3
>>>
>>> =============================
>>>
>>>
>>> Obviously that's not the expected response.  I expect to get something
>>> more like this:
>>>
>>> Bin: 10  !bin:  01
>>> Bin: 2  !bin:  1
>>>
>>>
>>> Can anyone shed some light on this for me?  The server is running an
>>> old version of OpenSUSE, and php --version returns:
>>>
>>
>> You probably need to read up on computer architechture and CS theory -
>> the ~ operator works fine, your understanding does not. What you're
>> looking at is the following:
>>
>> $bin = 2 = 00000000000000000000000000000010 (64 bit number)
>
> 32-bit number, as Colin pointed out.
>
> Peter
>
> --
> <hype>
> WWW: http://plphp.dk / http://plind.dk
> LinkedIn: http://www.linkedin.com/in/plind
> BeWelcome/Couchsurfing: Fake51
> Twitter: http://twitter.com/kafe15
> </hype>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux