Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Bitwise operation for WHERE clause

Bitwise operation for WHERE clause

Scheduled Pinned Locked Moved Database
databaseregexquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leftyfarrell
    wrote on last edited by
    #1

    I have a table with a bitmask value column and would like to select from this table where records have any match with another bitmask value. For instance 3 database records: ID, bitmask_value 1, 1 2, 3 3, 16 To check if a single bit is selected, I can: SELECT * FROM table WHERE (bitmask_value & 1) = 1 (checks if bit 1 is selected - returns ID 1 and ID 2) But I would like to get all records where a criteria bitmask value I define is selected, such as, user selects 1 and 2 = 3. I could do this: SELECT * FROM table WHERE ((bitmask_value & 1) = 1 OR (bitmask_value & 2) = 2) BUT if my criteria bitmask value includes several bits, can I query a different way to compare bitmask_value and criteria_bitmask, and get the proper results without checking each bit individually? So given a criteria_bitmask value of 3... can I query using the value 3 somehow and get the following: -any rows with 1 selected -any rows with 2 selected -any rows with 1 and 2 selected -any rows with 1 and any others selected -any rows with 2 and any others selected -any rows with 1 and 2 and any others selected I'm only filtering rows that DO NOT have a 1 or a 2 or both a 1 and a 2.

    C 1 Reply Last reply
    0
    • L Leftyfarrell

      I have a table with a bitmask value column and would like to select from this table where records have any match with another bitmask value. For instance 3 database records: ID, bitmask_value 1, 1 2, 3 3, 16 To check if a single bit is selected, I can: SELECT * FROM table WHERE (bitmask_value & 1) = 1 (checks if bit 1 is selected - returns ID 1 and ID 2) But I would like to get all records where a criteria bitmask value I define is selected, such as, user selects 1 and 2 = 3. I could do this: SELECT * FROM table WHERE ((bitmask_value & 1) = 1 OR (bitmask_value & 2) = 2) BUT if my criteria bitmask value includes several bits, can I query a different way to compare bitmask_value and criteria_bitmask, and get the proper results without checking each bit individually? So given a criteria_bitmask value of 3... can I query using the value 3 somehow and get the following: -any rows with 1 selected -any rows with 2 selected -any rows with 1 and 2 selected -any rows with 1 and any others selected -any rows with 2 and any others selected -any rows with 1 and 2 and any others selected I'm only filtering rows that DO NOT have a 1 or a 2 or both a 1 and a 2.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Sorry, some of your terminology is confusing me.

      Leftyfarrell wrote:

      1 and 2 = 3.

      Actually 1 AND 2 = 0. Did you mean 1 OR 2?

      Leftyfarrell wrote:

      SELECT * FROM table WHERE ((bitmask_value & 1) = 1 OR (bitmask_value & 2) = 2)

      What about: WHERE (bitmask_value & 3) <> 0

      bitmask_value result
      0 0
      1 1
      2 2
      3 3
      4 0
      5 1
      6 2
      7 3
      8 0
      etc.


      Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

      L 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Sorry, some of your terminology is confusing me.

        Leftyfarrell wrote:

        1 and 2 = 3.

        Actually 1 AND 2 = 0. Did you mean 1 OR 2?

        Leftyfarrell wrote:

        SELECT * FROM table WHERE ((bitmask_value & 1) = 1 OR (bitmask_value & 2) = 2)

        What about: WHERE (bitmask_value & 3) <> 0

        bitmask_value result
        0 0
        1 1
        2 2
        3 3
        4 0
        5 1
        6 2
        7 3
        8 0
        etc.


        Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

        L Offline
        L Offline
        Leftyfarrell
        wrote on last edited by
        #3

        I did mean 1 OR 2 selected. By saying 1 and 2 = 3, I meant that the search criteria is defined for me by a user selecting options, and if they select options 1 and 2, I need to pull anyone that matches 1 OR 2. Their interface assumes an OR condition on the options they select. I believe what you have provided is what I need. I just need to test a bit more. Thank-you.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups