Re: algorythm question
| From: | Bar��" Mert | Date: | Mon, 23 Sep 2002 22:14:05 +0000 |
| Subject: | Re: algorythm question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-117327@lists.php.net to get a copy of this message | ||
ok, now it's clear why i must use '|' instead of '+'.
i still couldn't get what happens with bits but you
don't have to explain more. i know that it's about
bitwise operators, and how they handle numbers. no!
ok! i just got it!
thank you again.
--- bob parker <bob_parker@dodo.com.au> wrote:
> Couple of points:
> You really do need to use the '!' operator to
> combine the categories not '+'
> eg if your user entered 2 twice you would get
> $userLevels = 2 + 2; // wrong we just made our admin
> a writer
> but
> $userLevels = 2 | 2; // ok is still admin because
> $userLevels is still 2
>
> These operators, | and & are called bit wise
> operators to see how they
> work look at the binary representations of the
> numbers
> say $userlevel = 15 then in binary
> $userlevel is 1111
> $result = 1111 & 0001; // $result is 0001 binary or
> decimal 1
> $result = 1111 & 0010; // $result is 0010 binary or
> decimal 2
> $result = 1111 & 0100; // $result is 0101 binary or
> decimal 4
> $result = 1111 & 1000; // $result is 1000 binary or
> decimal 8
> the '&' operator only returns the bit or bits which
> are both 1 in the
> same column.
>
> The or operator returns the bits which are 1 in
> ether column so
> 2 | 2 which is 0010 | 0010 still returns 0010 which
> is 2
> Hope this helps
> bob
__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com