Re: Bitwise NOT operator?

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

 



On Tue, 2010-08-24 at 12:24 -0400, Andy McKenzie wrote:

> On Tue, Aug 24, 2010 at 11:06 AM, Richard Quadling <rquadling@xxxxxxxxx> wrote:
> > On 20 August 2010 17:00, Andy McKenzie <amckenzie4@xxxxxxxxx> 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



[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