By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,249 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,249 IT Pros & Developers. It's quick & easy.

Attempting to use Bitwise Operators

P: n/a
I have a question regarding bitwise operators, I've been trying to
figure this out for about two days now, and I just can't seem to get it.
What I'm trying to do is use a variable to hold a bitmask of 'flags'
for users on a website. The function below is supposed to be a search
function for one of those flags in a particular thing.

The idea is that when something calls userId(), it should get to see
what type of user flags that are supported. Now I've moved things
around, and it seems like the only if that ever executes is the first
one, the code doesn't ever reach any of the elseif blocks.

I want the function to search for the first user that it finds has a bit
set in the bitmask and return the userId as supplied by the cookie that
represents that bitmask.

Optionally, userId() can be called with one of the arguments specifying
a particular usertype to look for that particular usertype. The user
can be logged into multiple sections of the site at a time, so if
they're entering something where they need the USER_ADMIN privilege, I
should be able to call userId(USER_ADMIN) and get their UID if they're
an admin and E_NOT_LOGGED_IN if they're not logged in as an admin. Same
goes for any of the options that are defined. Can anyone else see what
is wrong with this?

Thanks for the help in advance.
Constants defined for USER_*:
=============================

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

Code that isn't working:
========================

function userId($pref = USER_ANYBODY)
{
/*
* If the user is logged in, this will return their user ID.
* If they are not, then it won't. If more then one user is
* logged in, this function will prefer MEMPL and SEMPL types
* over any other, unless specified by $pref.
*/

$uT = userType();

if(userAccess($uT, $pref))
{
/*
* The usertype that we're looking for is logged in.
*/

if(($pref & USER_SEEKER) > 0)
{
$tmpCookie = $_COOKIE['seeker'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);

return($id);
}

elseif(($pref & USER_MEMPL) > 0)
{
$tmpCookie = $_COOKIE['mEmpl'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);

return($id);
}

elseif(($pref & USER_SEMPL) > 0)
{
$tmpCookie = $_COOKIE['sEmpl'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);

return($id);
}

elseif(($pref & USER_ADMIN) > 0)
{
$tmpCookie = $_COOKIE['admin'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);

return($id);
}
}

// Oops. We don't have anything to do, return E_NOT_LOGGED_IN.

return(E_NOT_LOGGED_IN);
}
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

On 8-Jul-2005, "Michael B. Trausch" <fd************@gmail.nospam.com>
wrote:
I have a question regarding bitwise operators, I've been trying to
figure this out for about two days now, and I just can't seem to get it.
What I'm trying to do is use a variable to hold a bitmask of 'flags'
for users on a website. The function below is supposed to be a search
function for one of those flags in a particular thing.

The idea is that when something calls userId(), it should get to see
what type of user flags that are supported. Now I've moved things
around, and it seems like the only if that ever executes is the first
one, the code doesn't ever reach any of the elseif blocks.

I want the function to search for the first user that it finds has a bit
set in the bitmask and return the userId as supplied by the cookie that
represents that bitmask.

Optionally, userId() can be called with one of the arguments specifying
a particular usertype to look for that particular usertype. The user
can be logged into multiple sections of the site at a time, so if
they're entering something where they need the USER_ADMIN privilege, I
should be able to call userId(USER_ADMIN) and get their UID if they're
an admin and E_NOT_LOGGED_IN if they're not logged in as an admin. Same
goes for any of the options that are defined. Can anyone else see what
is wrong with this?

Thanks for the help in advance.
Constants defined for USER_*:
=============================

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

Code that isn't working:
========================

function userId($pref = USER_ANYBODY)
{
/*
* If the user is logged in, this will return their user ID.
* If they are not, then it won't. If more then one user is
* logged in, this function will prefer MEMPL and SEMPL types
* over any other, unless specified by $pref.
*/

$uT = userType();

if(userAccess($uT, $pref))
{
/*
* The usertype that we're looking for is logged in.
*/

if(($pref & USER_SEEKER) > 0)
{
$tmpCookie = $_COOKIE['seeker'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);

return($id);
}

elseif(($pref & USER_MEMPL) > 0)
{
$tmpCookie = $_COOKIE['mEmpl'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);

return($id);
}

elseif(($pref & USER_SEMPL) > 0)
{
$tmpCookie = $_COOKIE['sEmpl'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);

return($id);
}

elseif(($pref & USER_ADMIN) > 0)
{
$tmpCookie = $_COOKIE['admin'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);

return($id);
}
}

// Oops. We don't have anything to do, return E_NOT_LOGGED_IN.

return(E_NOT_LOGGED_IN);
}


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.

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #2

P: n/a
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/languag...rs.bitwise.php the &
operator is bitwise AND, and according to
http://us3.php.net/manual/en/languag...rs.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/languag...rs.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.

- Mike
Jul 17 '05 #3

P: n/a
& is bitwise

$a= 9;
$b =9;
$c = 6;

$a & $b = 9;
$a & $c = 0;
&& is a logical and

$a && $b =1
$a && $c = 0;

"Tom Thackrey" <us***********@nospam.com> wrote in message
news:0c*****************@newssvr21.news.prodigy.co m...

On 8-Jul-2005, "Michael B. Trausch" <fd************@gmail.nospam.com>
wrote:
I have a question regarding bitwise operators, I've been trying to
figure this out for about two days now, and I just can't seem to get it.
What I'm trying to do is use a variable to hold a bitmask of 'flags'
for users on a website. The function below is supposed to be a search
function for one of those flags in a particular thing.

The idea is that when something calls userId(), it should get to see
what type of user flags that are supported. Now I've moved things
around, and it seems like the only if that ever executes is the first
one, the code doesn't ever reach any of the elseif blocks.

I want the function to search for the first user that it finds has a bit
set in the bitmask and return the userId as supplied by the cookie that
represents that bitmask.

Optionally, userId() can be called with one of the arguments specifying
a particular usertype to look for that particular usertype. The user
can be logged into multiple sections of the site at a time, so if
they're entering something where they need the USER_ADMIN privilege, I
should be able to call userId(USER_ADMIN) and get their UID if they're
an admin and E_NOT_LOGGED_IN if they're not logged in as an admin. Same
goes for any of the options that are defined. Can anyone else see what
is wrong with this?

Thanks for the help in advance.
Constants defined for USER_*:
=============================

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

Code that isn't working:
========================

function userId($pref = USER_ANYBODY)
{
/*
* If the user is logged in, this will return their user ID.
* If they are not, then it won't. If more then one user is
* logged in, this function will prefer MEMPL and SEMPL types
* over any other, unless specified by $pref.
*/

$uT = userType();

if(userAccess($uT, $pref))
{
/*
* The usertype that we're looking for is logged in.
*/

if(($pref & USER_SEEKER) > 0)
{
$tmpCookie = $_COOKIE['seeker'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);
return($id);
}

elseif(($pref & USER_MEMPL) > 0)
{
$tmpCookie = $_COOKIE['mEmpl'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);
return($id);
}

elseif(($pref & USER_SEMPL) > 0)
{
$tmpCookie = $_COOKIE['sEmpl'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);
return($id);
}

elseif(($pref & USER_ADMIN) > 0)
{
$tmpCookie = $_COOKIE['admin'];
list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);
return($id);
}
}

// Oops. We don't have anything to do, return E_NOT_LOGGED_IN.

return(E_NOT_LOGGED_IN);
}
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.

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

Jul 17 '05 #4

P: n/a

"Tom Thackrey" <us***********@nospam.com> wrote in message
news:0c*****************@newssvr21.news.prodigy.co m...
The problem is you aren't using the bitwise operators. & is not bitwise,
&&
is


No! ""&&" is "logical and". "&" is "bitwise and" -- the reverse of what
you are telling him. At least that is the way it is in C, C++, Java, etc.,
etc.

Shelly
Jul 17 '05 #5

P: n/a
On Fri, 08 Jul 2005 13:10:45 -0400, "Michael B. Trausch"
<fd************@gmail.nospam.com> wrote:
The idea is that when something calls userId(), it should get to see
what type of user flags that are supported. Now I've moved things
around, and it seems like the only if that ever executes is the first
one, the code doesn't ever reach any of the elseif blocks.


Umm yes, that's the point of *elseif*. If the first block executes,
the other ones won't. Replace all your elseif's with regular if's and
your code will work as indended!

Jul 17 '05 #6

P: n/a

On 8-Jul-2005, "Michael B. Trausch" <fd************@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/languag...rs.bitwise.php the &
operator is bitwise AND, and according to
http://us3.php.net/manual/en/languag...rs.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/languag...rs.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 ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #7

P: n/a
Try using the switch statement.

Switch($var){
case 1: do something;break;
case 2: do something;break;
default: do whatever something else;break
}

The rest is up to you !!

"Wayne" <no*@here.com> wrote in message
news:e5********************************@4ax.com...
On Fri, 08 Jul 2005 13:10:45 -0400, "Michael B. Trausch"
<fd************@gmail.nospam.com> wrote:
The idea is that when something calls userId(), it should get to see
what type of user flags that are supported. Now I've moved things
around, and it seems like the only if that ever executes is the first
one, the code doesn't ever reach any of the elseif blocks.


Umm yes, that's the point of *elseif*. If the first block executes,
the other ones won't. Replace all your elseif's with regular if's and
your code will work as indended!

Jul 17 '05 #8

P: n/a
Tom Thackrey wrote:

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:


Hrm. So then I wonder where it is wrong...

*sigh*

Thank you, though. I didn't think to write something like that to try
it out. My partial brain-dead error.

I think that I may just re-write the system to be slightly more simple
and less featureful for now, and if we have the time, go back and
reimplement something better, later.

Thanks,
Mike
Jul 17 '05 #9

P: n/a
Wayne wrote:
On Fri, 08 Jul 2005 13:10:45 -0400, "Michael B. Trausch"
<fd************@gmail.nospam.com> wrote:

The idea is that when something calls userId(), it should get to see
what type of user flags that are supported. Now I've moved things
around, and it seems like the only if that ever executes is the first
one, the code doesn't ever reach any of the elseif blocks.


Umm yes, that's the point of *elseif*. If the first block executes,
the other ones won't. Replace all your elseif's with regular if's and
your code will work as indended!


It's supposed to stop at the first one that is true, by design, which is
why ELSEIF was used. Apparently another portion of the code is feeding
garbage to this function, however, and this function is working as
designed. I didn't think to unit-test the function *seperate* from the
rest of the code, it just seemed by all indicators that this was the
code that was failing.

That's what I get for trying to code with little sleep. Whoops.

- Mike
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.