RE: [PHP] Bitwise NOT operator?
From: Richard Quadling > On 24 August 2010 21:42, Andy McKenzie wrote: >> On Tue, Aug 24, 2010 at 3:55 PM, Ford, Mike wrote: From: Andy McKenzie [mailto:amckenz...@gmail.com] >>> 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 ~: 0111" 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!) >>> >> >> 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. >> > I think trying to explain to someone with no knowledge of the rules is > going to be a little beyond the role of the PHP documentation. A > rudimentary understanding has to be assumed. > > You are talking about decimal numbers (2, 3, 4) and then applying the > NOT operator and then expressing the result in base 10 and base 2. > > Decimal numbers are column based. By worldwide and historic > convention, leading zeros are not needed. In fact, worldwide > convention has dictated that a leading 0 implies an octal number and > not a decimal one. > > Binary numbers are block based. Historic/worldwide convention dictates > bits are either singular (true/false) or in blocks (bytes, words, > double-words, quad-words, etc.) OK. Nibbles/nybbles/nybles too. > > You say a "two-bit number". Well, there is no such entity. As soon as > you talk in terms of bits, you are dealing in binary and this is block > based, not column based. > > Applying a not operator has the effect of inverting all the bits. We > see that perfectly fine in ... > > ~0001 = 1110 > > But, when you then express that pattern in decimal, the rules > regarding 2's compliment kick in. -128 to 127 = 256 options. Not -127 > to 127 ... what happened to -0? To make it simple, the computer hardware doesn't know or care if you want two bits or 128, so neither can PHP. If you are only interested in the lower bits, you need to mask your answer to throw away the rest. For example, doing a bitwise AND with 3 will discard all but the last two bits, 7 will give you the last three bits, etc. Bob McConnell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise NOT operator?
Please stop arguing this pointless topic on the php mailing list. Regards Peter -- WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise NOT operator?
On 24 August 2010 21:42, Andy McKenzie wrote: > On Tue, Aug 24, 2010 at 3:55 PM, Ford, Mike wrote: >>> -Original Message- >>> From: Andy McKenzie [mailto:amckenz...@gmail.com] >>> Sent: 24 August 2010 17:24 >>> To: php-general@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 ~: >>> 0111" >>> >>> 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 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > I think trying to explain to someone with no knowledge of the rules is going to be a little beyond the role of the PHP documentation. A rudimentary understanding has to be assumed. You are talking about decimal numbers (2, 3, 4) and then applying the NOT operator and then expressing the result in base 10 and base 2. Decimal numbers are column based. By worldwide and historic convention, leading zeros are not needed. In fact, worldwide convention has dictated that a leading 0 implies an octal number and not a decimal one. Binary numbers are block based. Historic/worldwide convention dictates bits are either singular (true/false) or in blocks (bytes, words, double-words, quad-words, etc.) OK. Nibbles/nybbles/nybles too. You say a "two-bit number". Well, there is no such entity. As soon as you talk in terms of bits, you are dealing in binary and this is block based, not column based. Applying a not operator has the effect of inverting all the bits. We see that perfectly fine in ... ~0001 = 1110 But, when you then express that pattern in decimal, the rules regarding 2's compliment kick in. -128 to 127 = 256 options. Not -127 to 127 ... what happened to -0? -- Richard Quadling. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise NOT operator?
On Tue, Aug 24, 2010 at 3:55 PM, Ford, Mike wrote: >> -Original Message- >> From: Andy McKenzie [mailto:amckenz...@gmail.com] >> Sent: 24 August 2010 17:24 >> To: php-general@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 ~: >> 0111" >> >> 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Bitwise NOT operator?
> -Original Message- > From: Andy McKenzie [mailto:amckenz...@gmail.com] > Sent: 24 August 2010 17:24 > To: php-general@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 ~: > 0111" > > 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.f...@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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise NOT operator?
On Tue, 2010-08-24 at 12:24 -0400, Andy McKenzie wrote: > On Tue, Aug 24, 2010 at 11:06 AM, Richard Quadling > wrote: > > On 20 August 2010 17:00, Andy McKenzie 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);" > > 1101 > > 1101 > > > > 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 "0011", > 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 ~: > 0111" > > 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
Re: [PHP] Bitwise NOT operator?
On Tue, Aug 24, 2010 at 11:06 AM, Richard Quadling wrote: > On 20 August 2010 17:00, Andy McKenzie 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);" > 1101 > 1101 > > 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 "0011", 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 ~: 0111" 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise NOT operator?
On 20 August 2010 17:00, Andy McKenzie 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);" 1101 1101 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise NOT operator?
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 wrote: > On 20 August 2010 17:41, Peter Lind wrote: >> On 20 August 2010 17:10, Andy McKenzie 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 >>> >>> >> >>> $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 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 = 0010 (64 bit number) > > 32-bit number, as Colin pointed out. > > Peter > > -- > > WWW: http://plphp.dk / http://plind.dk > LinkedIn: http://www.linkedin.com/in/plind > BeWelcome/Couchsurfing: Fake51 > Twitter: http://twitter.com/kafe15 > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise NOT operator?
On 20 August 2010 17:41, Peter Lind wrote: > On 20 August 2010 17:10, Andy McKenzie 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 >> >> > >> $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 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 = 0010 (64 bit number) 32-bit number, as Colin pointed out. Peter -- WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise NOT operator?
On 20 August 2010 17:10, Andy McKenzie 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 > > > $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 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 = 0010 (64 bit number) ~$bin = ~2 = -3 = 1101 (the inverse of the above) Computers generally do not operate on a couple of bits from a byte - they tend to operate on a byte, a word, a doubleword, etc (before any nitpickers interrupt: sure, they can, but most operations don't). Hence, you're not doing a not operation on just "10", you're doing it on "0010". And as you are operating on a signed int, you'll get a negative number out of the ~ operation (seeing as you flipped the most significant bit). Regards Peter -- WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php