[PHP] Re: Bitwise NOT operator?

2010-08-25 Thread Colin Guthrie
'Twas brillig, and Andy McKenzie at 24/08/10 21:42 did gyre and gimble:
 Even if I'd thought about it in terms of the architecture, I
 would have assumed that PHP would treat a two-bit number as a two-bit
 number

You two-bit hustler!

In all seriousness tho', where do you ever provide a two bit number?

$n = 2;

The above is a number, it's not implicitly a two bit number...

I mean, if I do:

$n = 2;
$n += 2;

What do I get then? $n == 0? That's what *should* happen if $n was
indeed two bits. It would overflow and wrap around.

Your number in $n is in fact represented by whatever the variable type
is. In this case it's a 32 bit integer number.

Col


-- 

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mandriva Linux Contributor [http://www.mandriva.com/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]


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



Re: [PHP] Re: Bitwise NOT operator?

2010-08-25 Thread Andy McKenzie
On Wed, Aug 25, 2010 at 9:46 AM, Colin Guthrie gm...@colin.guthr.ie wrote:
 'Twas brillig, and Andy McKenzie at 24/08/10 21:42 did gyre and gimble:
 Even if I'd thought about it in terms of the architecture, I
 would have assumed that PHP would treat a two-bit number as a two-bit
 number

 You two-bit hustler!

 In all seriousness tho', where do you ever provide a two bit number?

 $n = 2;

 The above is a number, it's not implicitly a two bit number...

 I mean, if I do:

 $n = 2;
 $n += 2;

 What do I get then? $n == 0? That's what *should* happen if $n was
 indeed two bits. It would overflow and wrap around.

 Your number in $n is in fact represented by whatever the variable type
 is. In this case it's a 32 bit integer number.

 Col


You're right, of course:  internally, PHP has to treat every number as
being some specific number of bits.  My point was that, not knowing a
lot about the low-level systems and internals, it's easy to assume
that something I enter as two bits (or three, or four, or whatever)
will be treated as such and returned as such.

My background isn't computer architecture:  what little relevant
experience I've had was two years in an electrical engineering program
(I decided it wasn't for me, and got out), but I never looked at
computer system architecture.  Based on my background, I tend to think
of a string of bits as just that -- a string of bits, basically
disconnected from anything else.  Therefore, if I feed five bits into
a NOT gate, I should get five bits back, and they should be the
opposite of what I fed it.  ~(10110) = 01001.  What I do with it after
that is up to me.  Math is different, and may require adding extra
bits, but a NOT isn't a mathematical function in my mind, it's a
function performed one bit at a time on exactly what you feed it.

As Gary points out, there ARE languages and places where that's doable
-- I've used the bit-field tool he mentioned, I think, although it was
a long time ago in that EE program -- so I had some reason for my
(incorrect) assumption.

However:  As Peter says, this is all fairly pointless, and totally
unnecessary on this list.  Colin gave me a meaningful answer in his
first response, and we've strayed pretty far from the topic.  At this
point, I'm not going to convince anyone of my view, and no one else is
going to convince me more than they have of their view, so let's let
the thread die in peace.  If other people want to continue the
discussion, go for it, but I'm going to try to keep quiet. 8-)

Thanks again to everyone who chimed in, though -- I appreciate the help.

-Alex McKenzie

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



[PHP] Re: Bitwise NOT operator?

2010-08-20 Thread Colin Guthrie
'Twas brillig, and Andy McKenzie at 20/08/10 16:10 did gyre and gimble:
 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:  1101
 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 output looks correct to me, so I think it's your expectations that
are incorrect.

You are assuming that you are working with a 2-bit number the in actual
fact you are working with a 32-bit number.

If you did

$bin = 2;
$notbin = ~$bin  3;

Then this should get the results you want. The bitwise  3 bit
essentially limits things to a 2-bit number. (3 in binary is represented
by 1's for the two LSBs and 0's for all other bits).

FWIW, the fact that ~2 == -3 is due to twos compliment binary notation:
http://en.wikipedia.org/wiki/Twos_Compliment

Col

-- 

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mandriva Linux Contributor [http://www.mandriva.com/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]


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