SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple Bitwise Question

    Hiya:

    If I want to set a bitwise value against another bitwise value I would use:
    PHP Code:
    $val_a $val_b 
    I'd like to to do the absolute opposite, something like (*incorrect syntax alert!*)
    PHP Code:
    $val_a !& $val_b 
    So that all bitwise values are set except these.
    The example is with a SQL Query:

    Display all Press Releases in the Tech Categories:
    PHP Code:
    $con_tech_tot 8704 // tech category 1 bits: 8704
    $pro_tech_tot 513 // tech category 2 bits: 513

    SELECT ID,RDate,Title 
            FROM ReleasesNew
            WHERE 
    ((TechCat1 & {$pro_tech_tot}) OR (TechCat2 & {$con_tech_tot}))"; 
    Using the $con_tech_xxx vars above, how would I alter the SQL above to display all Press Releases where $con_tech_xxx are not set in the database?

    Thanks a lot

  2. #2
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I'm not mistaking the '|' operator does the opposite of the '&' operator. But judging from the operator descriptions, it could also be the '^'.

    http://www.php.net/manual/en/languag...rs.bitwise.php

    If those don't work, you could always alter your query to this:
    ... WHERE NOT ((TechCat1 & .... or something like that

  3. #3
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot. I'll try the NOT option. I'm not sure why I didn't think of that myself...you get so bogged down in one path of thought you don't stop to think about another..

    Cheers!

  4. #4
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    & is bitwise AND. Bits are compared and if BOTH are 1 then the resulting bit is set.

    | is bitwise OR. Bits are compared and if EITHER one is 1 then the resulting bit is set.

    ^ is bitwise XOR. Bits are compared and if the bits are different (1 and 0) then the resulting bit is set.
    Erh

  5. #5
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help

    Although I wanted to know how you would write this if I was "comparing" two values, eg:

    I'm fine with this:
    Code:
    $x & $y
    I get fuzzy with this:
    Code:
    $x | $y
    How then would you write the above using ^ (XOR) and incorporate it into the SQL as in my original post? Something like:

    PHP Code:
    $sql "SELECT ID,RDate,Title 
      FROM ReleasesNew
      WHERE TO_DAYS(RDate) >= TO_DAYS(NOW()) - "
    .CONF_DAYS_REL."
      AND (RelRSprofessional ^ 
    {$pro_tech_tot})
      AND (RelRSconsumer ^ 
    {$con_tech_tot})
      AND OnSite = 1 "

    BTW using NOT before the parenthesed clauses' seems to work as suggested by Captain Proton. I'll try the XOR right now...

    Thanks a lot folks

  6. #6
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Not Supported

    I'm using MySQL 3.23-log. XOR isn't supported till version 4.02

    http://www.mysql.com/doc/en/Bit_functions.html#IDX1352



    I'll stick to using NOT.
    Thank you both for helping me out.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •