473,226 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Attempting to use Bitwise Operators

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
9 2496

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
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
& 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

"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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
4
by: Mike Hodkin | last post by:
As a beginning student of C++, books reference "bitwise operators" and give brief examples, but I have not read a good explanation of what they are used for. One reference mentioned that they are...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
2
by: Mark Rae | last post by:
Hi, This isn't *specifically* an ASP.NET question, so I've also posted it in the ADO.NET group - however, it's not too far off-topic... Imagine a SQL Server 2005 database with a table with an...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
16
by: Santhosh | last post by:
Hi to all, How the individual digits of a number can be obtained using the bitwise operators alone.Is it possible to do it ? If we have n = 34 Result has to be 3,4. Thanks a billion for...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.