homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

How do you do a bitwise compare in select where?

 5:45 am on Nov 18, 2010 (gmt 0)

Hi All,

I'm trying to select from a table by doing a bitwise compare but it does not work. Here is the sql select statement i have in my php script...

$Mask = implode($aom);

$sql = "SELECT * FROM dbtable WHERE ('".$Mask."' & Code)";

Basically; SELECT * FROM dbtable WHERE ('10100000000000' & Code)

$Mask is a 14 character string of ones and zeros i.e. "10100000000000"

Code is also a 14 character string field in the table similar to $Mask.

I want to do a bitwise compare of $Mask & Code with the "And" operator to test if Bits that are set in both $a and $b are set or true. if so, then get the record.



Like i said, the way i have it does not work. Is it because you can't do a bitwise compare on two strings?

A little help would be appreciated.



 8:24 pm on Nov 19, 2010 (gmt 0)

I don't do a lot with bitwise operators, but I think your query needs to look like this:

$sql = "SELECT * FROM dbtable WHERE '".$Mask."' & Code = '".$Mask."'";


 9:45 pm on Nov 19, 2010 (gmt 0)


While your solution won't work, you reminded me that ".$Mask. & code" must compare to something. Not "= .$Mask." but "<> '00000000000000'" for the where to work properly.

So the sql statement, i think, should look like this...

$Mask = '10100000000000';
$sql = "SELECT * FROM dbtable WHERE '".$Mask."' & code <> '"."00000000000000"."'";

As long as the bitwise operation of ".$Mask. & code" do not result in all zeros, i'll get records.

BTW, how did you get the php code to display in color?


 11:29 pm on Nov 19, 2010 (gmt 0)

unfortunately, it does not work because is appears the the bitwise and/or operators only work on integers not strings like you can in php.


 8:58 pm on Nov 23, 2010 (gmt 0)

What happens if you take out the single quotes? MySQL should then treat those as integers.

If your `Code` field is a VARCHAR (or other string) use the Cast functions [dev.mysql.com] to get it to the right type.


 11:47 pm on Nov 23, 2010 (gmt 0)


I give it try asap.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved