Reply to Re: Help With Bitwise Operations

Your name:

Reply:


Posted by ZeldorBlat on 12/02/07 02:54

On Dec 1, 9:30 pm, Rob Wilkerson <r.d.wilker...@gmail.com> wrote:
> I'm attempting to do some work around existing code that uses bitwise
> operations to manage flags passed into a function and I'm quite
> frankly unequipped to do so. I've never done much with bitwise
> operations and all the reading I've done today doesn't appear to be
> helping me much. I'm hoping someone here can provide a remedial
> lesson. Given the following constants:
>
> define('FLAG_1, 1);
> define('FLAG_2', 2);
> define('FLAG_3', 4);
> define('FLAG_4', 8);
> define('FLAG_5', 16);
> define('FLAG_6', 32);
> define('FLAG_7', 64);
> define('FLAG_8', 128);
> define('FLAG_9', 256);
> define('FLAG_10', 512);
> define('FLAG_11', 1024);
> define('FLAG_12', 2048);
>
> I'm making this call:
>
> $foo = my_function ( 'arg1', 'arg2', FLAG_1|FLAG_6|FLAG_4 );
>
> To this function signature:
>
> function my_function ( $arg1, $arg2='blah', $flags=NULL )
>
> Inside the function, if I just echo the value of $args I get "41".
>
> Is that correct? From my reading, it doesn't seem correct, but it may
> just be my ignorance in this area. Any assistance reading and
> understanding this stuff would be much appreciated.
>
> Thanks.
>
> Rob

FLAG_1 = 1 = 000001
FLAG_6 = 32 = 100000
FLAG_4 = 8 = 001000

If we OR them all together we get 101001 = 41. So that's correct.

Inside your function if you want to check if any particular flag was
set you would do it like this:

if($flags & FLAG_6) {
echo 'FLAG_6 was set';
}
else {
echo 'FLAG_6 was not set';
}

So, in your example, $flags & FLAG_6 would be true because:

$flags = 41 = 101001
FLAG_6 = 32 = 100000

If we AND them together we have 100000 which, inside an if statement,
evaluates to true.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация