SitePoint Sponsor

User Tag List

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

    Bitwise Operator Query

    Hey folks:

    I have 2 arrays: The keys are strings with integers (the bits) as values:

    PHP Code:
    $array_con = array(
    name1=>1,
    name2=>2,
    name3=>4,
    name4=>8,
    ); 
    and:

    PHP Code:
    $array_pro = array(
    name1=>1,
    name2=>2,
    name3=>4,
    name4=>8,
    ); 

    I want to use bitwise comparisons in a SQL query, to compare a single integer-value, itself a bitwise composition taken from a column in MySQL (3.22.32), with the values in the above array. Something like this:

    PHP Code:
    $sql .= "AND CatConsumer & {$sum_of_array_con_bits} AND CatProfessional & {$sum_of_array_pro_bits}"
    Basically: "do there exist values in either array that compare in a bitwise manner with the single value in my db?"

    But I get unpredictable results, which is why I'm making this posting . Is a bitwise comparison on the sum of the arrays' values the correct way of doing things? I should add that I'm no expert at bitwise comparisons...

    Can anyone suggest the correct method of performing this comparison? - cheers

  2. #2
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - I sussed it:

    I was doing the bitwise comparison correctly (using array_sum()) but the SQL wasn't strung together correctly - duh!

    here's what I did:

    PHP Code:
    $sql .= "AND ((CatConsumer & {$sum_of_array_con_bits}) OR (CatProfessional & {$sum_of_array_pro_bits})) "
    Just the correct arrangement of brackets and an OR clause. I hpe this helps someone sometime.

    Cheers anyway


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
  •