Re: Bitwise NOT operator?

From: Date: Fri, 20 Aug 2010 15:42:44 +0000
Subject: Re: Bitwise NOT operator?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-307615@lists.php.net to get a copy of this message
On 20 August 2010 17:41, Peter Lind <peter.e.lind@gmail.com> wrote: > On 20 August 2010 17:10, Andy McKenzie <amckenzie4@gmail.com> 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>

« previous php.general (#307615) next »