homepage Welcome to WebmasterWorld Guest from 54.204.94.228
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?
nelsonm

5+ Year Member



 
Msg#: 4231800 posted 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.

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.

 

whoisgregg

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



 
Msg#: 4231800 posted 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:

<?php
$sql = "SELECT * FROM dbtable WHERE '".$Mask."' & Code = '".$Mask."'";
?>

nelsonm

5+ Year Member



 
Msg#: 4231800 posted 9:45 pm on Nov 19, 2010 (gmt 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?

nelsonm

5+ Year Member



 
Msg#: 4231800 posted 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.

whoisgregg

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



 
Msg#: 4231800 posted 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.

nelsonm

5+ Year Member



 
Msg#: 4231800 posted 11:47 pm on Nov 23, 2010 (gmt 0)

Thanks...

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