Welcome to WebmasterWorld Guest from 54.196.2.131

Forum Moderators: open

Message Too Old, No Replies

How do you do a bitwise compare in select where?

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

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 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.

10100000000000=$Mask
10100000000001=Code
--------------
10100000000000=true


10100000000000=$Mask
00000100100000=Code
--------------
00000000000000=false

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)

Senior Member from US 

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Dec 9, 2003
posts:3416
votes: 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)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 0


thanks,

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...

<?php
$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)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 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)

Senior Member from US 

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Dec 9, 2003
posts:3416
votes: 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)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 0


Thanks...

I give it try asap.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members