View Single Post
  #5  
Old 04-05-2007, 02:37 PM
ndnet
Hill Giant
 
Join Date: Oct 2003
Posts: 105
Default

SQL supports bitwise operators, such as & | ^ ~ etc.

If you have a set of numbers and you want to match them via bitmasks, it's easy to use the bitwise operators to do so.

Example table "bittest" with column "mask" that has some ints: (btw trying to format these sucks)
Code:
     +------+
     | mask |
     +------+
     | 5 |
     | 2 |
     | 3 |
     | 8 |
     | 4 |
     +------+
Now you have a bitmask 0100 which is the number 4. You want all numbers that have the bit set to 1.

Code:
mysql> SELECT * FROM `bittest` WHERE `mask` & 4;
     +------+
     | mask |
     +------+
     | 5 | < -- 0101
     | 4 | < -- 0100
     +------+

Now with the same mask 0100, you want all numbers where that bit is 0.

Code:
mysql> SELECT * FROM `bittest` WHERE NOT `mask` & 4;
     +------+
     | mask |
     +------+
     | 2 | < -- 0010
     | 3 | < -- 0011
     | 8 | < -- 1000
     +------+

And so forth~
Reply With Quote