Re: [PHP] Bitwise AND for 31-st bit
On Tuesday 17 May 2011 22:06:34 David Harkness wrote: It appears that PHP is truncating the constant 0x8000 to be within MIN_INT and MAX_INT instead of as a bit field, but when shifting 1 31 it doesn't do apply any constraints. That's pretty typical of bit-manipulation: it will merrily slide 1 bits off either end. This explains why produces 0 as it's doing 0x8000 0x7FFF. It also explains the second tests. Yes, that's it! I slightly expanded test output and now it's clear that you are right: $tst1 = (1 31); $tst2 = 0x8000; $tst1_eq = $tst1 0x8000; $tst2_eq = $tst2 0x8000; $str1 = sprintf(%1$032b, $tst1); $str2 = sprintf(%1$032b, $tst2); print tst1=$tst1 ($str1), tst1_eq=$tst1_eq, tst1_type=.gettype($tst1).\n; print tst2=$tst2 ($str2), tst2_eq=$tst2_eq, tst2_type=.gettype($tst2).\n; produces this output: tst1=-2147483648 (1000), tst1_eq=0, tst1_type=integer tst2=2147483647 (0111), tst2_eq=2147483647, tst2_type=integer Now it is obvious to me that PHP 5.2 clamps explicit constants to MAX_INT. Weird, b... On 64-bit 5.3.3 I get tst1=2147483648, tst1_eq=2147483648, tst1_type=integer tst2=2147483648, tst2_eq=2147483648, tst2_type=integer If I try the 64-bit-equivalent code I get tst1=-9223372036854775808, tst1_eq=-9223372036854775808, tst1_type=integer tst2=9.22337203685E+18, tst2_eq=-9223372036854775808, tst2_type=double I get similar results with 5.3 on my amd64 host too. It works as it should, no weirdness. Glad to know that 5.3 get it fixed. Pity to me that I can not update my 5.2 on ARM board. -- Vitalii Demianets -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Bitwise AND for 31-st bit
Hello, all! I've encountered odd behavior of PHP regarding bitwise AND operation when dealing with 31-st bit, and kindly ask to give me some pointers. Consider the following snippet: $tst1 = (1 31); $tst2 = 0x8000; $tst1_eq = $tst1 0x8000; $tst2_eq = $tst2 0x8000; print tst1=$tst1, tst1_eq=$tst1_eq, tst1_type=.gettype($tst1).\n; print tst2=$tst2, tst2_eq=$tst2_eq, tst2_type=.gettype($tst2).\n; The output is: tst1=-2147483648, tst1_eq=0, tst1_type=integer tst2=2147483647, tst2_eq=2147483647, tst2_type=integer And I totally can not understand, why in the hell tst1_eq=0 ? (BTW I'm using PHP 5.2.14 on 32-bit CPU, if that matters) Sorry if this is well-known feature and have discussed before. In such a case please point me to the URL of the discussion. -- Vitalii Demianets -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise AND for 31-st bit
Hi, Hmm.. interesting... tst1=-2147483648, tst1_eq=-2147483648, tst1_type=integer tst2=2147483648, tst2_eq=-2147483648, tst2_type=double at me... PHP Version 5.3.3-1ubuntu9.5 (Apache 2.0 - i686) Valentine On Tue, May 17, 2011 at 5:19 PM, Vitalii Demianets vi...@nppfactor.kiev.uawrote: Hello, all! I've encountered odd behavior of PHP regarding bitwise AND operation when dealing with 31-st bit, and kindly ask to give me some pointers. Consider the following snippet: $tst1 = (1 31); $tst2 = 0x8000; $tst1_eq = $tst1 0x8000; $tst2_eq = $tst2 0x8000; print tst1=$tst1, tst1_eq=$tst1_eq, tst1_type=.gettype($tst1).\n; print tst2=$tst2, tst2_eq=$tst2_eq, tst2_type=.gettype($tst2).\n; The output is: tst1=-2147483648, tst1_eq=0, tst1_type=integer tst2=2147483647, tst2_eq=2147483647, tst2_type=integer And I totally can not understand, why in the hell tst1_eq=0 ? (BTW I'm using PHP 5.2.14 on 32-bit CPU, if that matters) Sorry if this is well-known feature and have discussed before. In such a case please point me to the URL of the discussion. -- Vitalii Demianets -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise AND for 31-st bit
On Tuesday 17 May 2011 18:31:00 Bálint Horváth wrote: Hi, Hmm.. interesting... tst1=-2147483648, tst1_eq=-2147483648, tst1_type=integer tst2=2147483648, tst2_eq=-2147483648, tst2_type=double at me... PHP Version 5.3.3-1ubuntu9.5 (Apache 2.0 - i686) That is what I expected too. There are 2 possibilities: 1) There was a bug in 5.2.14 which is fixed in 5.3.3; 2) There still is a bug in latest version of PHP which manifest itself only on ARM CPU (which I am working on). The worst thing is that I can not compile PHP 5.3.x for my board at the moment. So, I think, I should live with it, until I manage somehow to upgrade PHP installation for that board. -- Vitalii Demianets -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise AND for 31-st bit
It appears that PHP is truncating the constant 0x8000 to be within MIN_INT and MAX_INT instead of as a bit field, but when shifting 1 31 it doesn't do apply any constraints. That's pretty typical of bit-manipulation: it will merrily slide 1 bits off either end. This explains why produces 0 as it's doing 0x8000 0x7FFF. It also explains the second tests. On 64-bit 5.3.3 I get tst1=2147483648, tst1_eq=2147483648, tst1_type=integer tst2=2147483648, tst2_eq=2147483648, tst2_type=integer If I try the 64-bit-equivalent code I get tst1=-9223372036854775808, tst1_eq=-9223372036854775808, tst1_type=integer tst2=9.22337203685E+18, tst2_eq=-9223372036854775808, tst2_type=double David
Re: [PHP] Bitwise NOT operator?
On 24 August 2010 21:42, Andy McKenzie amckenz...@gmail.com wrote: On Tue, Aug 24, 2010 at 3:55 PM, Ford, Mike m.f...@leedsmet.ac.uk 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?
Please stop arguing this pointless topic on the php mailing list. Regards 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Bitwise NOT operator?
From: Richard Quadling On 24 August 2010 21:42, Andy McKenzie amckenz...@gmail.com wrote: On Tue, Aug 24, 2010 at 3:55 PM, Ford, Mike m.f...@leedsmet.ac.uk 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?
On 20 August 2010 17:00, Andy McKenzie amckenz...@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); 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?
On Tue, Aug 24, 2010 at 11:06 AM, Richard Quadling rquadl...@gmail.com wrote: On 20 August 2010 17:00, Andy McKenzie amckenz...@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); 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 Tue, 2010-08-24 at 12:24 -0400, Andy McKenzie wrote: On Tue, Aug 24, 2010 at 11:06 AM, Richard Quadling rquadl...@gmail.com wrote: On 20 August 2010 17:00, Andy McKenzie amckenz...@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); 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?
-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, Aug 24, 2010 at 3:55 PM, Ford, Mike m.f...@leedsmet.ac.uk 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
[PHP] Bitwise NOT operator?
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 server is running an old version of OpenSUSE, and php --version returns: PHP 5.2.5 with Suhosin-Patch 0.9.6.2 (cli) (built: Dec 12 2007 03:51:56) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies I get the same response on an Ubuntu 8.04 LTS server with PHP 5.2.4-2ubuntu5.10 with Suhosin-Patch 0.9.6.2. Thanks in advance, Alex McKenzie amckenz...@gmail.com -- 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 amckenz...@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: 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 -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 /hype -- 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 peter.e.l...@gmail.com wrote: On 20 August 2010 17:10, Andy McKenzie amckenz...@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: 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 -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 /hype -- 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 peter.e.l...@gmail.com wrote: On 20 August 2010 17:41, Peter Lind peter.e.l...@gmail.com wrote: On 20 August 2010 17:10, Andy McKenzie amckenz...@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: 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 -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Fwd: [PHP] Bitwise operation giving wrong results
-- Forwarded message -- From: sean greenslade [EMAIL PROTECTED] Date: Fri, Oct 31, 2008 at 11:22 PM Subject: Re: [PHP] Bitwise operation giving wrong results To: Yeti [EMAIL PROTECTED] Cool, thanks. It worked. I didn't know you typeset PHP like that. On Thu, Oct 30, 2008 at 3:42 AM, Yeti [EMAIL PROTECTED] wrote: Usually in PHP one does not take much care about the data types, but in this case you absoloodle have to. If you use bit operators on a character then its ascii number will be taken instead (how should a number based operation work with a string?) also if you pass on $_GET params directly into ay bitwise operation you might get some un-nice behaviour. So check them first. ?php $a = $b = false; if (is_numeric($_GET['b'].$_GET['a'])) { $a = (int)$_GET['a']; $b = (int)$_GET['b']; echo $a $b; } ? -- --Zootboy -- --Zootboy
Re: [PHP] Bitwise operation giving wrong results
Usually in PHP one does not take much care about the data types, but in this case you absoloodle have to. If you use bit operators on a character then its ascii number will be taken instead (how should a number based operation work with a string?) also if you pass on $_GET params directly into ay bitwise operation you might get some un-nice behaviour. So check them first. ?php $a = $b = false; if (is_numeric($_GET['b'].$_GET['a'])) { $a = (int)$_GET['a']; $b = (int)$_GET['b']; echo $a $b; } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Bitwise operation giving wrong results
I have the following code as a test: ?php $a = $_GET['a']; $b = $_GET['b']; echo $a .. $b . = ; $out = $a $b; echo $out; //echo 15 2; ? if I set a to 15 and b to 2 in the URL like so: test.php?a=15b=2 it outputs zero as the answer. When I run the hard-coded '' operation (the commented out echo), it returns 2. I'm stumped. -- --Zootboy
Re: [PHP] Bitwise operation giving wrong results
On Wed, 2008-10-29 at 20:01 -0400, sean greenslade wrote: I have the following code as a test: ?php $a = $_GET['a']; $b = $_GET['b']; echo $a .. $b . = ; $out = $a $b; echo $out; //echo 15 2; ? if I set a to 15 and b to 2 in the URL like so: test.php?a=15b=2 it outputs zero as the answer. When I run the hard-coded '' operation (the commented out echo), it returns 2. I'm stumped. In PHP a boolean true also equates to 1. You are using the operator though, instead of the which I think you're looking for. They both do different things. Sorry, it's late and I've been befriending mr Jack Daniels a little tonight, so forgive me if my answer is as close to what you were looking for as is 42 (life the universe and everything) to The Question. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operation giving wrong results
Ashley Sheridan wrote: On Wed, 2008-10-29 at 20:01 -0400, sean greenslade wrote: I have the following code as a test: ?php $a = $_GET['a']; $b = $_GET['b']; echo $a .. $b . = ; $out = $a $b; echo $out; //echo 15 2; ? if I set a to 15 and b to 2 in the URL like so: test.php?a=15b=2 it outputs zero as the answer. When I run the hard-coded '' operation (the commented out echo), it returns 2. I'm stumped. In PHP a boolean true also equates to 1. You are using the operator though, instead of the which I think you're looking for. You misunderstand the question I think. He's trying to do this: http://www.php.net/manual/en/language.operators.bitwise.php For the OP: Try casting $a and $b to int's: $a = (int)$_GET['a']; no idea if it'll help though. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators and check if an bit is NOT within the flag.
On Tue, July 4, 2006 7:35 am, Mathijs wrote: //Do if VALIDATE_CHECK1 is set BUT NOT when VALIDATE_CHECK3 is set. if ($flag2 self::VALIDATE_CHECK1 $flag2 ~self::VALIDATE_CHECK3) Did you check operator precedence for versus ? Perhaps you just need parentheses... I'm also not at all sure the ~self::VALIDATE_CHECK3 is doing what you want... Echo that out and see if it's the number you would expect... -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Bitwise operators and check if an bit is NOT within the flag.
Hello there. I am working with some bitwise Operators for validating some variables. Now i need to know if an certain bit is NOT set and an other bit IS set. Example. ?php const VALIDATE_CHECK1 = 1; const VALIDATE_CHECK2 = 2; const VALIDATE_CHECK3 = 4; const VALIDATE_ALL= 7; //--Example 1 - This works nice. $flag1 = self::VALIDATE_CHECK1; //Do if VALIDATE_CHECK1 is set if ($flag1 self::VALIDATE_CHECK1) { print 'Validate 1'; } //Do if VALIDATE_CHECK2 is set if ($flag1 self::VALIDATE_CHECK2) { print 'Validate 2'; } //--Example 2 - I want to check if VALIDATE_CHECK3 is not set and then continue. $flag2 = self::VALIDATE_ALL; //Do if VALIDATE_CHECK1 is set BUT NOT when VALIDATE_CHECK3 is set. if ($flag2 self::VALIDATE_CHECK1 $flag2 ~self::VALIDATE_CHECK3) { print 'Only Validate 1'; } //etc... ? This last example i can't seem to get to work. I Want to only do that when for example VALIDATE_CHECK3 is not within the $flag2. How can i do this? Thx in advanced. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators and check if an bit is NOT within the flag.
Mathijs wrote: Hello there. I am working with some bitwise Operators for validating some variables. Now i need to know if an certain bit is NOT set and an other bit IS set. Example. ?php const VALIDATE_CHECK1 = 1; const VALIDATE_CHECK2 = 2; const VALIDATE_CHECK3 = 4; const VALIDATE_ALL= 7; //--Example 1 - This works nice. $flag1 = self::VALIDATE_CHECK1; //Do if VALIDATE_CHECK1 is set if ($flag1 self::VALIDATE_CHECK1) { print 'Validate 1'; } //Do if VALIDATE_CHECK2 is set if ($flag1 self::VALIDATE_CHECK2) { print 'Validate 2'; } //--Example 2 - I want to check if VALIDATE_CHECK3 is not set and then continue. $flag2 = self::VALIDATE_ALL; //Do if VALIDATE_CHECK1 is set BUT NOT when VALIDATE_CHECK3 is set. if ($flag2 self::VALIDATE_CHECK1 $flag2 ~self::VALIDATE_CHECK3) { class Test { const VALIDATE_CHECK1 = 1; const VALIDATE_CHECK2 = 2; const VALIDATE_CHECK3 = 4; const VALIDATE_ALL= 7; static function check($flag2) { if (($flag2 self::VALIDATE_CHECK1) !($flag2 self::VALIDATE_CHECK3)) print Only Validate 1; } } echo First attempt: ; Test::check( Test::VALIDATE_ALL ); echo \nSecond attempt: ; Test::check( Test::VALIDATE_CHECK1 ); echo \n; print 'Only Validate 1'; } //etc... ? This last example i can't seem to get to work. I Want to only do that when for example VALIDATE_CHECK3 is not within the $flag2. How can i do this? Thx in advanced. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.
Jochem Maas wrote: Mathijs wrote: Hello there. I am working with some bitwise Operators for validating some variables. Now i need to know if an certain bit is NOT set and an other bit IS set. Example. ?php const VALIDATE_CHECK1 = 1; const VALIDATE_CHECK2 = 2; const VALIDATE_CHECK3 = 4; const VALIDATE_ALL= 7; //--Example 1 - This works nice. $flag1 = self::VALIDATE_CHECK1; //Do if VALIDATE_CHECK1 is set if ($flag1 self::VALIDATE_CHECK1) { print 'Validate 1'; } //Do if VALIDATE_CHECK2 is set if ($flag1 self::VALIDATE_CHECK2) { print 'Validate 2'; } //--Example 2 - I want to check if VALIDATE_CHECK3 is not set and then continue. $flag2 = self::VALIDATE_ALL; //Do if VALIDATE_CHECK1 is set BUT NOT when VALIDATE_CHECK3 is set. if ($flag2 self::VALIDATE_CHECK1 $flag2 ~self::VALIDATE_CHECK3) { class Test { const VALIDATE_CHECK1 = 1; const VALIDATE_CHECK2 = 2; const VALIDATE_CHECK3 = 4; const VALIDATE_ALL= 7; static function check($flag2) { if (($flag2 self::VALIDATE_CHECK1) !($flag2 self::VALIDATE_CHECK3)) print Only Validate 1; } } echo First attempt: ; Test::check( Test::VALIDATE_ALL ); echo \nSecond attempt: ; Test::check( Test::VALIDATE_CHECK1 ); echo \n; print 'Only Validate 1'; } //etc... ? This last example i can't seem to get to work. I Want to only do that when for example VALIDATE_CHECK3 is not within the $flag2. How can i do this? Thx in advanced. Thank you very much. This seems to work :). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.
Mathijs wrote: Jochem Maas wrote: Mathijs wrote: ... Thank you very much. This seems to work :). cool. heres's a couple of funcs that might help you to understand bitwise operations better: ?php /* whether there is only 1 single bit set or not */ function single_bit_set(/*int*/ $i) { // beware: if $i is zero !($i ~get_ls1bit($i)) returns true; return $i ? !($i ~get_ls1bit($i)): false; } /* return the value of the least significant bit */ function get_ls1bit(/*int*/ $i) { for ($j = 1; $i !($i $j); $j = 1); return $i ? $j : 0; } /* return the value of the most significant bit */ function get_ms1bit(/*int*/ $i) { $x = 0; for ($j = $i; $i !($j == 1); $j = 1) { $x++; } return $i ? $j = $x: 0; } /* doesn't break when exponents are near the wordsize * of the machine as the native xor does, here is some example code to * illustrate: php -r ' // include function definition here $x = 3851235679; $y = 43814; echo \nThis is the value we want; echo \n3851262585; echo \nThe result of a native xor operation on integer values is treated as a signed integer; echo \n.($x ^ $y); echo \nWe therefore perform the MSB separately; echo \n.get_xor32($x, $y).\n; ' */ function get_xor32(/*int*/ $a, /*int*/ $b) { $a1 = $a 0x7FFF; $a2 = $a 0x; $a3 = $a 0x8000; $b1 = $b 0x7FFF; $b2 = $b 0x; $b3 = $b 0x8000; $c = ($a3 != $b3) ? 0x8000 : 0; return (($a1 ^ $b1) |($a2 ^ $b2)) + $c; } function get_bit_str($var, $safety = 0) { $rtn = ''; $var = intval($var); if ($var 0) { $var = 0 - $var; } while ($var != 0 /* $safety 31*/) { $rtn .= ($var 1); $var = 1; $safety++; } return $rtn; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.
Jochem Maas wrote: Mathijs wrote: Jochem Maas wrote: Mathijs wrote: ... Thank you very much. This seems to work :). cool. heres's a couple of funcs that might help you to understand bitwise operations better: ?php /* whether there is only 1 single bit set or not */ function single_bit_set(/*int*/ $i) { // beware: if $i is zero !($i ~get_ls1bit($i)) returns true; return $i ? !($i ~get_ls1bit($i)): false; } /* return the value of the least significant bit */ function get_ls1bit(/*int*/ $i) { for ($j = 1; $i !($i $j); $j = 1); return $i ? $j : 0; } /* return the value of the most significant bit */ function get_ms1bit(/*int*/ $i) { $x = 0; for ($j = $i; $i !($j == 1); $j = 1) { $x++; } return $i ? $j = $x: 0; } /* doesn't break when exponents are near the wordsize * of the machine as the native xor does, here is some example code to * illustrate: php -r ' // include function definition here $x = 3851235679; $y = 43814; echo \nThis is the value we want; echo \n3851262585; echo \nThe result of a native xor operation on integer values is treated as a signed integer; echo \n.($x ^ $y); echo \nWe therefore perform the MSB separately; echo \n.get_xor32($x, $y).\n; ' */ function get_xor32(/*int*/ $a, /*int*/ $b) { $a1 = $a 0x7FFF; $a2 = $a 0x; $a3 = $a 0x8000; $b1 = $b 0x7FFF; $b2 = $b 0x; $b3 = $b 0x8000; $c = ($a3 != $b3) ? 0x8000 : 0; return (($a1 ^ $b1) |($a2 ^ $b2)) + $c; } function get_bit_str($var, $safety = 0) { $rtn = ''; $var = intval($var); if ($var 0) { $var = 0 - $var; } while ($var != 0 /* $safety 31*/) { $rtn .= ($var 1); $var = 1; $safety++; } return $rtn; } Thx alot :). It will sure help me understanding it better :). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.
Jochem Maas wrote: Mathijs wrote: Jochem Maas wrote: Mathijs wrote: ... Thank you very much. This seems to work :). cool. heres's a couple of funcs that might help you to understand bitwise operations better: ?php /* whether there is only 1 single bit set or not */ function single_bit_set(/*int*/ $i) { // beware: if $i is zero !($i ~get_ls1bit($i)) returns true; return $i ? !($i ~get_ls1bit($i)): false; } /* return the value of the least significant bit */ function get_ls1bit(/*int*/ $i) { for ($j = 1; $i !($i $j); $j = 1); return $i ? $j : 0; } /* return the value of the most significant bit */ function get_ms1bit(/*int*/ $i) { $x = 0; for ($j = $i; $i !($j == 1); $j = 1) { $x++; } return $i ? $j = $x: 0; } /* doesn't break when exponents are near the wordsize * of the machine as the native xor does, here is some example code to * illustrate: php -r ' // include function definition here $x = 3851235679; $y = 43814; echo \nThis is the value we want; echo \n3851262585; echo \nThe result of a native xor operation on integer values is treated as a signed integer; echo \n.($x ^ $y); echo \nWe therefore perform the MSB separately; echo \n.get_xor32($x, $y).\n; ' */ function get_xor32(/*int*/ $a, /*int*/ $b) { $a1 = $a 0x7FFF; $a2 = $a 0x; $a3 = $a 0x8000; $b1 = $b 0x7FFF; $b2 = $b 0x; $b3 = $b 0x8000; $c = ($a3 != $b3) ? 0x8000 : 0; return (($a1 ^ $b1) |($a2 ^ $b2)) + $c; } function get_bit_str($var, $safety = 0) { $rtn = ''; $var = intval($var); if ($var 0) { $var = 0 - $var; } while ($var != 0 /* $safety 31*/) { $rtn .= ($var 1); $var = 1; $safety++; } return $rtn; } Thx alot :). It will sure help me understanding it better :). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.
Jochem Maas wrote: Mathijs wrote: Jochem Maas wrote: Mathijs wrote: ... Thank you very much. This seems to work :). cool. heres's a couple of funcs that might help you to understand bitwise operations better: ?php /* whether there is only 1 single bit set or not */ function single_bit_set(/*int*/ $i) { // beware: if $i is zero !($i ~get_ls1bit($i)) returns true; return $i ? !($i ~get_ls1bit($i)): false; } /* return the value of the least significant bit */ function get_ls1bit(/*int*/ $i) { for ($j = 1; $i !($i $j); $j = 1); return $i ? $j : 0; } /* return the value of the most significant bit */ function get_ms1bit(/*int*/ $i) { $x = 0; for ($j = $i; $i !($j == 1); $j = 1) { $x++; } return $i ? $j = $x: 0; } /* doesn't break when exponents are near the wordsize * of the machine as the native xor does, here is some example code to * illustrate: php -r ' // include function definition here $x = 3851235679; $y = 43814; echo \nThis is the value we want; echo \n3851262585; echo \nThe result of a native xor operation on integer values is treated as a signed integer; echo \n.($x ^ $y); echo \nWe therefore perform the MSB separately; echo \n.get_xor32($x, $y).\n; ' */ function get_xor32(/*int*/ $a, /*int*/ $b) { $a1 = $a 0x7FFF; $a2 = $a 0x; $a3 = $a 0x8000; $b1 = $b 0x7FFF; $b2 = $b 0x; $b3 = $b 0x8000; $c = ($a3 != $b3) ? 0x8000 : 0; return (($a1 ^ $b1) |($a2 ^ $b2)) + $c; } function get_bit_str($var, $safety = 0) { $rtn = ''; $var = intval($var); if ($var 0) { $var = 0 - $var; } while ($var != 0 /* $safety 31*/) { $rtn .= ($var 1); $var = 1; $safety++; } return $rtn; } Thx alot :). It will sure help me understanding it better :). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.
Jochem Maas wrote: Mathijs wrote: Jochem Maas wrote: Mathijs wrote: ... Thank you very much. This seems to work :). cool. heres's a couple of funcs that might help you to understand bitwise operations better: ?php /* whether there is only 1 single bit set or not */ function single_bit_set(/*int*/ $i) { // beware: if $i is zero !($i ~get_ls1bit($i)) returns true; return $i ? !($i ~get_ls1bit($i)): false; } /* return the value of the least significant bit */ function get_ls1bit(/*int*/ $i) { for ($j = 1; $i !($i $j); $j = 1); return $i ? $j : 0; } /* return the value of the most significant bit */ function get_ms1bit(/*int*/ $i) { $x = 0; for ($j = $i; $i !($j == 1); $j = 1) { $x++; } return $i ? $j = $x: 0; } /* doesn't break when exponents are near the wordsize * of the machine as the native xor does, here is some example code to * illustrate: php -r ' // include function definition here $x = 3851235679; $y = 43814; echo \nThis is the value we want; echo \n3851262585; echo \nThe result of a native xor operation on integer values is treated as a signed integer; echo \n.($x ^ $y); echo \nWe therefore perform the MSB separately; echo \n.get_xor32($x, $y).\n; ' */ function get_xor32(/*int*/ $a, /*int*/ $b) { $a1 = $a 0x7FFF; $a2 = $a 0x; $a3 = $a 0x8000; $b1 = $b 0x7FFF; $b2 = $b 0x; $b3 = $b 0x8000; $c = ($a3 != $b3) ? 0x8000 : 0; return (($a1 ^ $b1) |($a2 ^ $b2)) + $c; } function get_bit_str($var, $safety = 0) { $rtn = ''; $var = intval($var); if ($var 0) { $var = 0 - $var; } while ($var != 0 /* $safety 31*/) { $rtn .= ($var 1); $var = 1; $safety++; } return $rtn; } Thx alot :). It will sure help me understanding it better :). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Bitwise operators
Hello, From php manual: $a $b Shift leftShift the bits of $a $b steps to the left (each step means multiply by two) $a $b Shift rightShift the bits of $a $b steps to the right (each step means divide by two) So i ask what this output? $a = 4; $b = 3; echo $a $b; echo $a $b; Angelo -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Bitwise operators
[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] on Monday, September 26, 2005 9:18 AM said: So i ask what this output? $a = 4; $b = 3; echo $a $b; echo $a $b; You just spent 3-5 minutes writing an email and now almost 10 minutes waiting for a reply to something that would have taken you 2 minutes to test on your own. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators
On 9/26/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: From php manual: $a $b Shift leftShift the bits of $a $b steps to the left (each step means multiply by two) $a $b Shift rightShift the bits of $a $b steps to the right (each step means divide by two) So i ask what this output? $a = 4; $b = 3; echo $a $b; 32 echo $a $b; 0 So your program will output '320'. What was the problem again? -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators
I tested; I don't want to waste peoples time. Rewriting the question: $a = 4; $b = 3; $c = $a $b; $d = $a $b; echo c = . $c . br; echo d = . $d . br; this outputs: c = 32 d = 0 The question is why? Angelo - Original Message - From: Chris W. Parker [EMAIL PROTECTED] To: php-general@lists.php.net Sent: Monday, September 26, 2005 1:28 PM Subject: RE: [PHP] Bitwise operators [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] on Monday, September 26, 2005 9:18 AM said: So i ask what this output? $a = 4; $b = 3; echo $a $b; echo $a $b; You just spent 3-5 minutes writing an email and now almost 10 minutes waiting for a reply to something that would have taken you 2 minutes to test on your own. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators
I tested; I don't want to waste peoples time. Rewriting the question: this outputs: c = 32 d = 0 The question is why? First row is the bit's number and the second row is the bit's value: #8 | #7 | #6 | #5 | #4 | #3 | #2 | #1 --- 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 So your variable $a, which has a value of 4 starts out in the bit position of #3 with that bit turned on.. Shift it left 3 (value of $b) spaces and you end up with bit #6 getting turned on giving you a value of 32, which is what $c is echoing out. Shifting to the right 3 spaces from the same starting position nets you 0 with all the bits turned off. At least, I'm pretty sure that's right. thnx, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operators
The second value is the number of spaces to shift, dint realize that. Thanks for your time Chris. Angelo - Original Message - From: Chris Boget [EMAIL PROTECTED] To: [EMAIL PROTECTED]; php-general@lists.php.net Sent: Monday, September 26, 2005 2:02 PM Subject: Re: [PHP] Bitwise operators I tested; I don't want to waste peoples time. Rewriting the question: this outputs: c = 32 d = 0 The question is why? First row is the bit's number and the second row is the bit's value: #8 | #7 | #6 | #5 | #4 | #3 | #2 | #1 --- 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 So your variable $a, which has a value of 4 starts out in the bit position of #3 with that bit turned on.. Shift it left 3 (value of $b) spaces and you end up with bit #6 getting turned on giving you a value of 32, which is what $c is echoing out. Shifting to the right 3 spaces from the same starting position nets you 0 with all the bits turned off. At least, I'm pretty sure that's right. thnx, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Bitwise operations criticism needed
The situation. Im currently using a home brewed groups permission code in my site, but for limited users/groups its ok. Beyond that, the code will take the fast road to hell. I started to look in depth at bitwise operations today, and after much googling, and looking at other code, came up with this mock up, of a basic permissions routine (not neccessarilly the final product), that utilizes bitwise operations. Im looking for advise, to see if Im heading in the right way, and/or improvements with this. Thanks for your time... -- start code -- ?php $perm = $user = array(); $perm_total = 0; // Permissions from some source like a database/file $perm['execute'] = 1; $perm['write'] = 2; $perm['read']= 4; // User permissions (predetermined) from sessions maybe $user['tom']= 7; // rwx $user['joe']= 5; // rx $user['dirty_harry']= 9; // illegal in this case?? $user['whats_his_face'] = 6; // rw // Set the sum of source permissions foreach($perm as $value) { $perm_total |= $value; } echo ul; // Loop over the users foreach($user as $id = $user_perm) { // User permissions should be between 1 7, else set it to 0 if ($user_perm $perm_total || $user_perm 0) { $user_perm = 0; } // Compare user bits to permission bits $compare = $perm_total $user_perm; // Make it an even 4 bit string (for visual effect) $bits_string = sprintf(%03d, decbin( $compare )); echo liUser: . $id . /li; // Check to see if the comparision contains any permission bits $can_read= (($compare $perm['read']) == $perm['read']) === TRUE ? TRUE : FALSE; $can_write = (($compare $perm['write']) == $perm['write']) === TRUE ? TRUE : FALSE; $can_execute = (($compare $perm['execute']) == $perm['execute']) === TRUE ? TRUE : FALSE; echo ul; echo li . $user_perm . ' - ' . $bits_string . /li; echo liCan Read: $can_read/li; echo liCan Write: $can_write/li; echo liCan Execute: $can_execute/li; echo /ul; } echo /ul; ? -- end code -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operations criticism needed
Gerard Samuel wrote: The situation. Im currently using a home brewed groups permission code in my site, but for limited users/groups its ok. Beyond that, the code will take the fast road to hell. I started to look in depth at bitwise operations today, and after much googling, and looking at other code, came up with this mock up, of a basic permissions routine (not neccessarilly the final product), that utilizes bitwise operations. Im looking for advise, to see if Im heading in the right way, and/or improvements with this. Thanks for your time... -- start code -- ?php $perm = $user = array(); $perm_total = 0; // Permissions from some source like a database/file $perm['execute'] = 1; $perm['write'] = 2; $perm['read']= 4; // User permissions (predetermined) from sessions maybe $user['tom']= 7; // rwx $user['joe']= 5; // rx $user['dirty_harry']= 9; // illegal in this case?? 9 is 1001, so if you ignore fourth bit, it's equal to 1. Some day you might want to add other permission rights using other bits. $user['whats_his_face'] = 6; // rw // Set the sum of source permissions foreach($perm as $value) { $perm_total |= $value; } echo ul; // Loop over the users foreach($user as $id = $user_perm) { These 2 pieces of code: // User permissions should be between 1 7, else set it to 0 if ($user_perm $perm_total || $user_perm 0) { $user_perm = 0; } and // Compare user bits to permission bits $compare = $perm_total $user_perm; do the same thing, and are quite useless, the code will work without it. If you extend the rights one day, you would have to modify it. // Make it an even 4 bit string (for visual effect) $bits_string = sprintf(%03d, decbin( $compare )); echo liUser: . $id . /li; // Check to see if the comparision contains any permission bits $can_read= (($compare $perm['read']) == $perm['read']) === TRUE ? TRUE : FALSE; $can_write = (($compare $perm['write']) == $perm['write']) === TRUE ? TRUE : FALSE; $can_execute = (($compare $perm['execute']) == $perm['execute']) === TRUE ? TRUE : FALSE; echo ul; echo li . $user_perm . ' - ' . $bits_string . /li; echo liCan Read: $can_read/li; echo liCan Write: $can_write/li; echo liCan Execute: $can_execute/li; echo /ul; } echo /ul; ? -- end code -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operations criticism needed
Marek Kilimajer wrote: Gerard Samuel wrote: The situation. Im currently using a home brewed groups permission code in my site, but for limited users/groups its ok. Beyond that, the code will take the fast road to hell. I started to look in depth at bitwise operations today, and after much googling, and looking at other code, came up with this mock up, of a basic permissions routine (not neccessarilly the final product), that utilizes bitwise operations. Im looking for advise, to see if Im heading in the right way, and/or improvements with this. Thanks for your time... -- start code -- ?php $perm = $user = array(); $perm_total = 0; // Permissions from some source like a database/file $perm['execute'] = 1; $perm['write'] = 2; $perm['read']= 4; // User permissions (predetermined) from sessions maybe $user['tom']= 7; // rwx $user['joe']= 5; // rx $user['dirty_harry']= 9; // illegal in this case?? 9 is 1001, so if you ignore fourth bit, it's equal to 1. Some day you might want to add other permission rights using other bits. Yes I may use it some day. I was trying to simulate an illegal permission. So how should I handle this, if I should handle this??? $user['whats_his_face'] = 6; // rw // Set the sum of source permissions foreach($perm as $value) { $perm_total |= $value; } echo ul; // Loop over the users foreach($user as $id = $user_perm) { These 2 pieces of code: // User permissions should be between 1 7, else set it to 0 if ($user_perm $perm_total || $user_perm 0) { $user_perm = 0; } and // Compare user bits to permission bits $compare = $perm_total $user_perm; do the same thing, and are quite useless, the code will work without it. If you extend the rights one day, you would have to modify it. Ok, I understand what you mean about the if() statement, as its related to your previous comment above. But isn't $compare = $perm_total $user_perm; needed for this to work? // Make it an even 4 bit string (for visual effect) $bits_string = sprintf(%03d, decbin( $compare )); echo liUser: . $id . /li; // Check to see if the comparision contains any permission bits $can_read= (($compare $perm['read']) == $perm['read']) === TRUE ? TRUE : FALSE; $can_write = (($compare $perm['write']) == $perm['write']) === TRUE ? TRUE : FALSE; $can_execute = (($compare $perm['execute']) == $perm['execute']) === TRUE ? TRUE : FALSE; echo ul; echo li . $user_perm . ' - ' . $bits_string . /li; echo liCan Read: $can_read/li; echo liCan Write: $can_write/li; echo liCan Execute: $can_execute/li; echo /ul; } echo /ul; ? -- end code -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operations criticism needed
Gerard Samuel wrote: Marek Kilimajer wrote: Gerard Samuel wrote: The situation. Im currently using a home brewed groups permission code in my site, but for limited users/groups its ok. Beyond that, the code will take the fast road to hell. I started to look in depth at bitwise operations today, and after much googling, and looking at other code, came up with this mock up, of a basic permissions routine (not neccessarilly the final product), that utilizes bitwise operations. Im looking for advise, to see if Im heading in the right way, and/or improvements with this. Thanks for your time... -- start code -- ?php $perm = $user = array(); $perm_total = 0; // Permissions from some source like a database/file $perm['execute'] = 1; $perm['write'] = 2; $perm['read']= 4; // User permissions (predetermined) from sessions maybe $user['tom']= 7; // rwx $user['joe']= 5; // rx $user['dirty_harry']= 9; // illegal in this case?? 9 is 1001, so if you ignore fourth bit, it's equal to 1. Some day you might want to add other permission rights using other bits. Yes I may use it some day. I was trying to simulate an illegal permission. So how should I handle this, if I should handle this??? Your checks are something like if($user['tom'] $perm['read']) echo 'Tom can read'; Only the 3rd bit is checked, all others are ignored and won't do any harm. Anyway, the clean way of setting permissions is: $user['tom'] = $perm['execute'] | $perm['write'] | $perm['read']; $user['whats_his_face'] = 6; // rw // Set the sum of source permissions foreach($perm as $value) { $perm_total |= $value; } echo ul; // Loop over the users foreach($user as $id = $user_perm) { These 2 pieces of code: // User permissions should be between 1 7, else set it to 0 if ($user_perm $perm_total || $user_perm 0) { $user_perm = 0; } and // Compare user bits to permission bits $compare = $perm_total $user_perm; do the same thing, and are quite useless, the code will work without it. If you extend the rights one day, you would have to modify it. Ok, I understand what you mean about the if() statement, as its related to your previous comment above. But isn't $compare = $perm_total $user_perm; needed for this to work? $compare = $perm_total $user_perm; unsets all bits other than 1, 2 and 4, so 0 = $compare = 7. However, your first condition already did this. // Make it an even 4 bit string (for visual effect) $bits_string = sprintf(%03d, decbin( $compare )); echo liUser: . $id . /li; // Check to see if the comparision contains any permission bits $can_read= (($compare $perm['read']) == $perm['read']) === TRUE ? TRUE : FALSE; $can_write = (($compare $perm['write']) == $perm['write']) === TRUE ? TRUE : FALSE; $can_execute = (($compare $perm['execute']) == $perm['execute']) === TRUE ? TRUE : FALSE; echo ul; echo li . $user_perm . ' - ' . $bits_string . /li; echo liCan Read: $can_read/li; echo liCan Write: $can_write/li; echo liCan Execute: $can_execute/li; echo /ul; } echo /ul; ? -- end code -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise operations criticism needed
Marek Kilimajer wrote: Your checks are something like if($user['tom'] $perm['read']) echo 'Tom can read'; Only the 3rd bit is checked, all others are ignored and won't do any harm. Anyway, the clean way of setting permissions is: $user['tom'] = $perm['execute'] | $perm['write'] | $perm['read']; Understood. It cant be illegal if its set as you described. $compare = $perm_total $user_perm; unsets all bits other than 1, 2 and 4, so 0 = $compare = 7. However, your first condition already did this. Ok. So here is the updated code - -- start code -- ?php $perm = $user = array(); $perm_total = 0; // Permissions from some source like a database/file $perm['execute'] = 1; $perm['write'] = 2; $perm['read']= 4; // User permissions (predetermined) from sessions maybe $user['tom']= $perm['read'] | $perm['write'] | $perm['execute']; // rwx $user['joe']= $perm['read'] | $perm['execute']; // rx $user['whats_his_face'] = $perm['read'] | $perm['write']; // rw // Set the sum of source permissions foreach($perm as $value) { $perm_total |= $value; } echo ul; // Loop over the users foreach($user as $id = $user_perm) { // Compare user bits to permission bits $compare = $perm_total $user_perm; // Make it an even 4 bit string (for visual effect) $bits_string = sprintf(%03d, decbin( $compare )); echo liUser: . $id . /li; // Check to see if the comparision contains any permission bits $can_read= (($compare $perm['read']) == $perm['read']) === TRUE ? TRUE : FALSE; $can_write = (($compare $perm['write']) == $perm['write']) === TRUE ? TRUE : FALSE; $can_execute = (($compare $perm['execute']) == $perm['execute']) === TRUE ? TRUE : FALSE; echo ul; echo li . $user_perm . ' - ' . $bits_string . /li; echo liCan Read: $can_read/li; echo liCan Write: $can_write/li; echo liCan Execute: $can_execute/li; echo /ul; } echo /ul; ? -- end code -- So if thats it, now its time to expand on this idea on my end... Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise flagging in mysql database
On Tuesday 10 June 2003 06:08, Mike Mannakee wrote: I have a script that is collecting a bunch of information, and storing this in a database. However, the rows are kinda big and I'm thinking of packing the information into flag bits, and storing this information in one string in the database. I'm wondering if I should use mysql's bitwise operators to manipulate the data, or just pull it out clean and manipulate it in php. Does anyone have any experience using either mysql or php to do bitwise flagging operations? What kind of bitwise flag operations? Have you considered using MySQL's 'SET' field type? -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality. -- Albert Einstein */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Bitwise flagging in mysql database
I have a script that is collecting a bunch of information, and storing this in a database. However, the rows are kinda big and I'm thinking of packing the information into flag bits, and storing this information in one string in the database. I'm wondering if I should use mysql's bitwise operators to manipulate the data, or just pull it out clean and manipulate it in php. Does anyone have any experience using either mysql or php to do bitwise flagging operations? Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Bitwise operator question
Hi, I'm reading the description of Bitwise Operators on page 81 of Professional PHP 4, the Wrox book. In the highlighted example on that page, the line of code... $user_permissions = CREATE_RECORDS | ALTER_RECORDS; the description in the book says that this line is building a set of user permissions out of the previously created constants with the OR operator (I understand what OR means). The value of $user_permissions is set to either 1 or 4, which is in fact 5 (0101). But how is this single line doing that? The explanation was cryptic (to me). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bitwise
probably priority issue. try: if (($One $Two) == $One) - - in your example this means: ((0100 ) == 0100) (0100 == 0100) TRUE ...in other case 0100 ( == 0100) is 0100 0 is always 0 hope this helps Buchholz Fred wrote From: Fred [EMAIL PROTECTED] Date: Mon, 19 Nov 2001 18:02:53 -0800 Subject: [PHP] Bitwise I have 17 boolean variables which need to be stored in a MySQL database. I created a SET Column to store each of the boolean values as bits in the column. I should be able to test for the truth of a particular variable by checking if the corresponding bit is set. For example, if the first four variables are set (i.e. Column = a,b,c,d) then the numeric value of the column is 15 or . To test for c I would check to see if the third bit (4 or 100) is set. I chose to do the check this way: if ($One $Two == $One) echo True; This does not seem to work in many instances. Take the example above: 4 and 15 should equal 4 0100 and equals 0100 For some reason, my script gives as the result of 0100 and . It is fairly clear that this is not the correct behavior of the bitwise operator. Oddly enough, in some instances is works as expected: 16 and 17 should equal 16 1 and 10001 equals 1 My script has no problem generating the expected result here. Perhaps this problem arises from the loosely typed nature of PHP variables, but I do not see how it could. Any pointers would be appreciated. Fred -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Bitwise
I have 17 boolean variables which need to be stored in a MySQL database. I created a SET Column to store each of the boolean values as bits in the column. I should be able to test for the truth of a particular variable by checking if the corresponding bit is set. For example, if the first four variables are set (i.e. Column = a,b,c,d) then the numeric value of the column is 15 or . To test for c I would check to see if the third bit (4 or 100) is set. I chose to do the check this way: if ($One $Two == $One) echo True; This does not seem to work in many instances. Take the example above: 4 and 15 should equal 4 0100 and equals 0100 For some reason, my script gives as the result of 0100 and . It is fairly clear that this is not the correct behavior of the bitwise operator. Oddly enough, in some instances is works as expected: 16 and 17 should equal 16 1 and 10001 equals 1 My script has no problem generating the expected result here. Perhaps this problem arises from the loosely typed nature of PHP variables, but I do not see how it could. Any pointers would be appreciated. Fred -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] bitwise AND is acting strange
The users on my website all have an access number that is used to give them access to different parts of the site. Each bit represents a different part of the site. So, if a user has an access of 10, which is 1010 in binary, they have access to the parts of the site that are represented by the second and fourth bit. You know, standard bit masking stuff. So the part of the site that is represented by the second bit has a value of 2 (0010), and the part that is represented by the fourth bit has a value of 8 (1000) BUT, for some reason when I do (2 10) its giving me a result of zero, when I believe it should be doing (0010 1010) and giving me an answer of 0010 which is 2 in decimal. With users with an access other than 10, say 9, or 8, or 7, it seems to behave normally. What is going on? Is it treating the 10 as a binary 2? These access values are stored in a mysql table as a standard INT, and they are not UNSIGNED or BINARY. Am I missing something? Jeremy -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] bitwise comparison
How can I store a large number, value over 8 billion for bitwise comparison? I have a large set of switchs, getting up to 2 pow 34, and it goes outside the size of an int, can't set a type that will work. Nicholas Burke Strategic Profits Inc. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Bitwise solution?
On Thursday 01 March 2001 20:58, you wrote: But that would give me a value of a variable variable. And I need a numerical value so $var1 ="joe"; $var2 = date( "U" ); echo "$var1$var2"; won't do me any good. Ahhh. You want to use the mcrypt functions or simply crypt() ? -- Christian Reiniger LGDC Webmaster (http://sunsite.dk/lgdc/) The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence. - Edsger W. Dijkstra -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Bitwise solution?
I have 2 values. The first is a constant while the second is not. Is the first value a numerical value (real/integer?) or a string value? String. This is what I've come up with and it seems to work pretty ok. What do you guys think? (test code to illustrate a point; other than the XORing, not what I'm actually going to do with the "live" code) script language="php" $uniqueArray = array(); for( $i = 0; $i = 100; $i++ ) { $certnum = date( "U" ); // what the second variable is always set to if( $certnum == $lastCertNum ) { sleep( 1 ); } $uid = ( $uid == "nsa000" ) ? "mdb000" : "nsa000"; // sample first variable $uniqueNum = ( hexdec( $uid ) ^ $certnum ); echo "$uid ^ $certnum = $uniqueNumbr\n"; if( in_array( $uniqueNum, $uniqueArray )) { echo "Not a unique number!brbr\n"; } $uniqueArray[] = $uniqueNum; flush(); $lastCertNum = $certnum; } /script Chris
[PHP] Bitwise solution?
I'm wondering how I can do the following (if it is possible at all): I have 2 values. The first is a constant while the second is not. The second value, in this case, is a unix time stamp and as such will change every time it is set, down to the second. I need to somehow merge the two values so that the new, merged value would be as (semi) unique as the timestamp value. I was thinking I could do something like this: ( var1 var2 ) But that generates an identical value every time. Is there a way I can do this? And no, I cannot just use the timestamp value. :P And microtime() isn't really an option either. :( Any help at all would be greatly appreciated! :) Chris
RE: [PHP] Bitwise solution?
on 3/1/01 02:34 PM, [EMAIL PROTECTED] split open and melted thusly: I was thinking I could do something like this: ( var1 var2 ) i think what you want is: ${$var1$var2} But that would give me a value of a variable variable. And I need a numerical value so $var1 ="joe"; $var2 = date( "U" ); echo "$var1$var2"; won't do me any good. :/ I've a very odd predicament... Chris
Re: [PHP] Bitwise solution?
Hi, "Boget, Chris" wrote: I have 2 values. The first is a constant while the second is not. Is the first value a numerical value (real/integer?) or a string value? If it is numerical, then one of the things you could do for using a numerical value of (say) less than 100 could be: $finalVar = ($var2 * 10) + $var2; This is one of the simplest things you could do and meant only as a rough example. A faster way would be to right-shift by 4 (if that can be done in PHP) instead of multiplying by 10. This well essentially multiply the number by 16. I am relatively new to PHP, so the syntax I have used may not be correct. Sorry about that. Does this come anywhere close to what you would like to do? -- Regards, Harshdeep Singh Jawanda. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]