Attempting to use Bitwise Operators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael B. Trausch

    #1

    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);
    }
  • Tom Thackrey

    #2
    Re: Attempting to use Bitwise Operators


    On 8-Jul-2005, "Michael B. Trausch" <fdZEROman+spam @gmail.nospam.c om>
    wrote:
    [color=blue]
    > 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);
    > }[/color]

    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

    tom (at) creative (dash) light (dot) com
    do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

    Comment

    • Michael B. Trausch

      #3
      Re: Attempting to use Bitwise Operators

      Tom Thackrey wrote:[color=blue]
      >
      > 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.
      >[/color]

      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

      Comment

      • Simon Bridgewater

        #4
        Re: Attempting to use Bitwise Operators

        & 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" <use.signature@ nospam.com> wrote in message
        news:0czze.3199 $6%2.1520@newss vr21.news.prodi gy.com...[color=blue]
        >
        > On 8-Jul-2005, "Michael B. Trausch" <fdZEROman+spam @gmail.nospam.c om>
        > wrote:
        >[color=green]
        > > 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(':',[/color][/color]
        $tmpCookie);[color=blue][color=green]
        > >
        > > return($id);
        > > }
        > >
        > > elseif(($pref & USER_MEMPL) > 0)
        > > {
        > > $tmpCookie = $_COOKIE['mEmpl'];
        > > list($id, $i, $i, $i, $i, $i, $i) = explode(':',[/color][/color]
        $tmpCookie);[color=blue][color=green]
        > >
        > > return($id);
        > > }
        > >
        > > elseif(($pref & USER_SEMPL) > 0)
        > > {
        > > $tmpCookie = $_COOKIE['sEmpl'];
        > > list($id, $i, $i, $i, $i, $i, $i) = explode(':',[/color][/color]
        $tmpCookie);[color=blue][color=green]
        > >
        > > return($id);
        > > }
        > >
        > > elseif(($pref & USER_ADMIN) > 0)
        > > {
        > > $tmpCookie = $_COOKIE['admin'];
        > > list($id, $i, $i, $i, $i, $i, $i) = explode(':',[/color][/color]
        $tmpCookie);[color=blue][color=green]
        > >
        > > return($id);
        > > }
        > > }
        > >
        > > // Oops. We don't have anything to do, return E_NOT_LOGGED_IN .
        > >
        > > return(E_NOT_LO GGED_IN);
        > > }[/color]
        >
        > The problem is you aren't using the bitwise operators. & is not bitwise,[/color]
        &&[color=blue]
        > 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 jamesbutler@wil lglen.net (it's reserved for spammers)[/color]


        Comment

        • Shelly

          #5
          Re: Attempting to use Bitwise Operators


          "Tom Thackrey" <use.signature@ nospam.com> wrote in message
          news:0czze.3199 $6%2.1520@newss vr21.news.prodi gy.com...
          [color=blue]
          > The problem is you aren't using the bitwise operators. & is not bitwise,
          > &&
          > is[/color]

          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


          Comment

          • Wayne

            #6
            Re: Attempting to use Bitwise Operators

            On Fri, 08 Jul 2005 13:10:45 -0400, "Michael B. Trausch"
            <fdZEROman+spam @gmail.nospam.c om> wrote:
            [color=blue]
            >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.[/color]

            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!

            Comment

            • Tom Thackrey

              #7
              Re: Attempting to use Bitwise Operators


              On 8-Jul-2005, "Michael B. Trausch" <fdZEROman+spam @gmail.nospam.c om>
              wrote:
              [color=blue]
              > Tom Thackrey wrote:[color=green]
              > >
              > > 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.
              > >[/color]
              >
              > 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.[/color]

              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

              tom (at) creative (dash) light (dot) com
              do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

              Comment

              • Simon Bridgewater

                #8
                Re: Attempting to use Bitwise Operators

                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" <not@here.com > wrote in message
                news:e5ptc15pq2 o1ikflnjn9b6j84 kecq35es5@4ax.c om...[color=blue]
                > On Fri, 08 Jul 2005 13:10:45 -0400, "Michael B. Trausch"
                > <fdZEROman+spam @gmail.nospam.c om> wrote:
                >[color=green]
                > >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.[/color]
                >
                > 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!
                >[/color]


                Comment

                • Michael B. Trausch

                  #9
                  Re: Attempting to use Bitwise Operators

                  Tom Thackrey wrote:[color=blue]
                  >
                  > 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:
                  >[/color]

                  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

                  Comment

                  • Michael B. Trausch

                    #10
                    Re: Attempting to use Bitwise Operators

                    Wayne wrote:[color=blue]
                    > On Fri, 08 Jul 2005 13:10:45 -0400, "Michael B. Trausch"
                    > <fdZEROman+spam @gmail.nospam.c om> wrote:
                    >
                    >[color=green]
                    >>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.[/color]
                    >
                    > 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!
                    >[/color]

                    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

                    Comment

                    Working...