473,652 Members | 3,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ADM IN) 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_NO BODY', 0x00000000);
define('USER_SE EKER', 0x00000001);
define('USER_ME MPL', 0x00000002);
define('USER_SE MPL', 0x00000004);
define('USER_AD MIN', 0x00000008);
define('USER_AN YBODY', 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_LO GGED_IN);
}
Jul 17 '05 #1
9 2526

On 8-Jul-2005, "Michael B. Trausch" <fd************ @gmail.nospam.c om>
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_ADM IN) 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_NO BODY', 0x00000000);
define('USER_SE EKER', 0x00000001);
define('USER_ME MPL', 0x00000002);
define('USER_SE MPL', 0x00000004);
define('USER_AD MIN', 0x00000008);
define('USER_AN YBODY', 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_LO GGED_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*********@wil lglen.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 misunderstandin g 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******** *********@newss vr21.news.prodi gy.com...

On 8-Jul-2005, "Michael B. Trausch" <fd************ @gmail.nospam.c om>
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_ADM IN) 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_NO BODY', 0x00000000);
define('USER_SE EKER', 0x00000001);
define('USER_ME MPL', 0x00000002);
define('USER_SE MPL', 0x00000004);
define('USER_AD MIN', 0x00000008);
define('USER_AN YBODY', 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_LO GGED_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*********@wil lglen.net (it's reserved for spammers)

Jul 17 '05 #4

"Tom Thackrey" <us***********@ nospam.com> wrote in message
news:0c******** *********@newss vr21.news.prodi gy.com...
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.c om> 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.c om>
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 misunderstandin g 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_NO BODY', 0x00000000);
define('USER_SE EKER', 0x00000001);
define('USER_ME MPL', 0x00000002);
define('USER_SE MPL', 0x00000004);
define('USER_AD MIN', 0x00000008);
define('USER_AN YBODY', 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_SE EKER');
}
elseif(($pref & USER_MEMPL) > 0)
{
return('USER_ME MPL');
}
elseif(($pref & USER_SEMPL) > 0)
{
return('USER_SE MPL');
}
elseif(($pref & USER_ADMIN) > 0)
{
return('USER_AD MIN');
}
}
return('no hit');
}

echo userId(USER_NOB ODY),'<br>';
echo userId(USER_SEE KER),'<br>';
echo userId(USER_MEM PL),'<br>';
echo userId(USER_SEM PL),'<br>';
echo userId(USER_ADM IN),'<br>';
echo userId(USER_ANY BODY),'<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*********@wil lglen.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.c om...
On Fri, 08 Jul 2005 13:10:45 -0400, "Michael B. Trausch"
<fd************ @gmail.nospam.c om> 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.c om> 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
9008
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 anyone would use them - any real world examples or ideas? Examples follow (that I am reading in my Core JavaScript Guide 1.5). 15 & 9 yields 9 (1111 & 1001 = 1001) 15 | 9 yields 15 (1111 | 1001 = 1111) 15 ^ 9 yields 6 (1111 ^ 1001 = 0110) in...
4
2480
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 used in hardware programming. Are they used very often in routine C/C++ programming, and what for? thanks, MJH
6
8971
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 programming in straight C, the Win32 API sends windows messages to applications with various settings stored in bit fields, in which case changing the settings of the message is a matter of using the bitwise operators. This was a good practical example of...
2
3462
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 with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
37
11055
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 anyone that could tell me how to convert these two things? 1)
5
5777
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 you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why...
2
2744
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 int column used for bitwise data - you know the sort of thing... 0, 1, 2, 4, 8, 16 etc intBitwise strName ----------------------
29
5936
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 often done in Python because of keyword arguments and dicts, which are lot more versatile. Another major use, talking to hardware, is not something oft done in Python either. It seems like this occasional usage wouldn't justify having built-in...
16
2997
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 your reply in advance.
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.