From: Richard Quadling on
On 20 August 2010 17:00, Andy McKenzie <amckenzie4(a)gmail.com> wrote:
>  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.

There is no padding.

php -r "echo decbin(~2), PHP_EOL, decbin(-3);"
11111111111111111111111111111101
11111111111111111111111111111101

That's 100% correct and proper. Not "padded".

The example in [1] regarding E_ALL & ~E_NOTICE is a perfect example.

Richard.

[1] http://docs.php.net/manual/en/language.operators.bitwise.php
--
Richard Quadling.
From: Andy McKenzie on
On Tue, Aug 24, 2010 at 11:06 AM, Richard Quadling <rquadling(a)gmail.com> wrote:
> On 20 August 2010 17:00, Andy McKenzie <amckenzie4(a)gmail.com> wrote:
>>  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.
>
> There is no padding.
>
> php -r "echo decbin(~2), PHP_EOL, decbin(-3);"
> 11111111111111111111111111111101
> 11111111111111111111111111111101
>
> That's 100% correct and proper. Not "padded".
>
> The example in [1] regarding E_ALL & ~E_NOTICE is a perfect example.
>
> Richard.
>
> [1] http://docs.php.net/manual/en/language.operators.bitwise.php
> --
> Richard Quadling.
>


If I feed it "10" and it assumes "00000000000000000000000000000011",
it's padding the number with zeros.

From your example, this would have shown me what I needed to know:

"Then taking the value of E_NOTICE...
1000
.... and inverting it via ~:
11111111111111111111111111110111"

As it was, I assumed the 32-bit number was there because the author
wanted it there, not because PHP assumes those extra bits. As other
people have said, it probably would have been obvious if I'd known
more about how the system dealt with binary numbers, but coming from
my background, it wasn't at all obvious. Not a big deal, and I can
work with it now that I know what's going on, but it would have taken
me a long time to figure it out on my own, if I ever had. The tests I
was doing probably would have led me to the right answer eventually,
but Colin's original answer gave me the explanation and the solution
much faster.

-Alex
From: Ashley Sheridan on
On Tue, 2010-08-24 at 12:24 -0400, Andy McKenzie wrote:

> On Tue, Aug 24, 2010 at 11:06 AM, Richard Quadling <rquadling(a)gmail.com> wrote:
> > On 20 August 2010 17:00, Andy McKenzie <amckenzie4(a)gmail.com> wrote:
> >> 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.
> >
> > There is no padding.
> >
> > php -r "echo decbin(~2), PHP_EOL, decbin(-3);"
> > 11111111111111111111111111111101
> > 11111111111111111111111111111101
> >
> > That's 100% correct and proper. Not "padded".
> >
> > The example in [1] regarding E_ALL & ~E_NOTICE is a perfect example.
> >
> > Richard.
> >
> > [1] http://docs.php.net/manual/en/language.operators.bitwise.php
> > --
> > Richard Quadling.
> >
>
>
> If I feed it "10" and it assumes "00000000000000000000000000000011",
> it's padding the number with zeros.
>
> From your example, this would have shown me what I needed to know:
>
> "Then taking the value of E_NOTICE...
> 1000
> ... and inverting it via ~:
> 11111111111111111111111111110111"
>
> As it was, I assumed the 32-bit number was there because the author
> wanted it there, not because PHP assumes those extra bits. As other
> people have said, it probably would have been obvious if I'd known
> more about how the system dealt with binary numbers, but coming from
> my background, it wasn't at all obvious. Not a big deal, and I can
> work with it now that I know what's going on, but it would have taken
> me a long time to figure it out on my own, if I ever had. The tests I
> was doing probably would have led me to the right answer eventually,
> but Colin's original answer gave me the explanation and the solution
> much faster.
>
> -Alex
>


It isn't padded, you just have to understand that this is how binary
numbers are dealt with by computers. The missing numbers are implied,
and when you perform binary math on them, they are used.

I would recommend reading up on binary math if you plan to use it more,
as it will reveal any more surprises like this before you encounter
them.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: "Ford, Mike" on
> -----Original Message-----
> From: Andy McKenzie [mailto:amckenzie4(a)gmail.com]
> Sent: 24 August 2010 17:24
> To: php-general(a)lists.php.net
> Subject: Re: [PHP] Bitwise NOT operator?


> From your example, this would have shown me what I needed to know:
>
> "Then taking the value of E_NOTICE...
> 1000
> ... and inverting it via ~:
> 11111111111111111111111111110111"
>
> As it was, I assumed the 32-bit number was there because the author
> wanted it there, not because PHP assumes those extra bits.

That's not PHP. That's the underlying computer architecture, and PHP has no choice in the matter. (Well, assuming you leave BCMath and so on out of the equation!)

Cheers!

Mike
--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507 City Campus,
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: m.ford(a)leedsmet.ac.uk
Tel: +44 113 812 4730





To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
From: Andy McKenzie on
On Tue, Aug 24, 2010 at 3:55 PM, Ford, Mike <M.Ford(a)leedsmet.ac.uk> wrote:
>> -----Original Message-----
>> From: Andy McKenzie [mailto:amckenzie4(a)gmail.com]
>> Sent: 24 August 2010 17:24
>> To: php-general(a)lists.php.net
>> Subject: Re: [PHP] Bitwise NOT operator?
>
>
>> From your example, this would have shown me what I needed to know:
>>
>> "Then taking the value of E_NOTICE...
>> 1000
>> ... and inverting it via ~:
>> 11111111111111111111111111110111"
>>
>> As it was, I assumed the 32-bit number was there because the author
>> wanted it there, not because PHP assumes those extra bits.
>
> That's not PHP. That's the underlying computer architecture, and PHP has no choice in the matter. (Well, assuming you leave BCMath and so on out of the equation!)
>
> Cheers!
>
> Mike


True, but largely irrelevant from my point of view: I'm talking to
PHP. 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, even if it had to do some weirdness in the background because
it's not. If I enter a decimal two, I know the computer deals with it
as binary, and now I know it probably deals with it as a 32-bit binary
number, but it doesn't show me all the extra bits: it just shows me a
two.

My point here, much as it might sound like it, isn't that PHP is wrong
for doing things the way it does. Even if I thought it is, I don't
know what I'm talking about, and I know it. What I'm saying is that
the documentation doesn't even begin to indicate to people like me
that things won't work the way we expect. Maybe that's not necessary;
certainly I've never needed it until now, and the confusion was easily
cleared up. But adding to the docs might avoid a lot of confusion for
the next guy who doesn't really know what he's doing.

-Alex