Reply to Re: Attempting to use Bitwise Operators

Your name:

Reply:


Posted by Tom Thackrey on 07/09/05 00:12

On 8-Jul-2005, "Michael B. Trausch" <fdZEROman+spam@gmail.nospam.com>
wrote:

> Tom Thackrey wrote:
> >
> > The problem is you aren't using the bitwise operators. & is not bitwise,
> > &&
> > is
> >
> > Also, you don't need the >0, if ($pref && USER_ADMIN) will work just
> > fine
> > and won't have a problem if you use the high order bit as a flag.
> >
>
> Am I misreading the documentation, then? According to
> http://us3.php.net/manual/en/language.operators.bitwise.php the &
> operator is bitwise AND, and according to
> http://us3.php.net/manual/en/language.operators.logical.php the &&
> operator is logical AND.
>
> Or, am I misunderstanding the definition of logical AND in PHP? I don't
> want (TRUE and TRUE), I want (0xffffffff & 0x00000001) where the output
> of the expression should be 0x00000001 (1) and hence greater then zero,
> returning true.
>
> The way I am understanding the published documentation, which states the
> following, is that the above should work. There appears, however to be
> a flaw in that logic:
>
> $a & $b And Bits that are set in both $a and $b are set.
> (from http://us3.php.net/manual/en/language.operators.bitwise.php)
>
> In other words, the way I read the documentation, it should be the same
> operators that you use in C (&& expression evaluation, and & for bitmask
> comparisons).
>
> Incidentally, if I replace & with && in the code, it still doesn't work
> properly. If the user running the code is USER_SEEKER, it works just
> fine, returning the user ID as it should and then the page that I'm
> working on, well, works. However, if the user is anything other then
> USER_SEEKER, the function fails to return anything but an apparently
> empty value. If the user has no cookie, it works as it should, although
> I do not know if this is consequental or incidental to something else.

Sorry, braincheck. You are right.

In pentance I tested your code. It looks like the problem is not with the
code you posted. I didn't have all your code, so here's what I tried:

<html>
<body>
<?php

define('USER_NOBODY', 0x00000000);
define('USER_SEEKER', 0x00000001);
define('USER_MEMPL', 0x00000002);
define('USER_SEMPL', 0x00000004);
define('USER_ADMIN', 0x00000008);
define('USER_ANYBODY', 0xffffffff);


function userId($pref = USER_ANYBODY)
{
if(1 || userAccess($uT, $pref)) // this has been no-opd to true.
{
/*
* The usertype that we're looking for is logged in.
*/

echo $pref,'<br>';
if(($pref & USER_SEEKER) > 0)
{
return('USER_SEEKER');
}
elseif(($pref & USER_MEMPL) > 0)
{
return('USER_MEMPL');
}
elseif(($pref & USER_SEMPL) > 0)
{
return('USER_SEMPL');
}
elseif(($pref & USER_ADMIN) > 0)
{
return('USER_ADMIN');
}
}
return('no hit');
}

echo userId(USER_NOBODY),'<br>';
echo userId(USER_SEEKER),'<br>';
echo userId(USER_MEMPL),'<br>';
echo userId(USER_SEMPL),'<br>';
echo userId(USER_ADMIN),'<br>';
echo userId(USER_ANYBODY),'<br>';

?>
</body>
</html>

output:
0
no hit
1
USER_SEEKER
2
USER_MEMPL
4
USER_SEMPL
8
USER_ADMIN
4294967295
USER_SEEKER



--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to jamesbutler@willglen.net (it's reserved for spammers)

[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

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