Re: Bitwise operators and check if an bit is NOT within the flag.
| From: | Jochem Maas | Date: | Tue, 04 Jul 2006 13:02:11 +0000 |
| Subject: | Re: Bitwise operators and check if an bit is NOT within the flag. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-239118@lists.php.net to get a copy of this message | ||
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.
>