0

Suppose you have two binary values:

001011 and 001111

How can you get the number of different bits in MySQL? I tried:

SELECT BIT_COUNT(BINARY  001011 ^ BINARY 001111)

This returns 6.I mean how to implement it? I means either i need from fetching mysql query or from php script?Please help.

2 Answers2

0

perform sql operation with query

SELECT BIT_COUNT(BINARY  001011 ^ BINARY 001111) as bitcount

then print the result,say $result['bitcount']

arunrc
  • 628
  • 2
  • 14
  • 26
  • It says Parse error: syntax error, unexpected 'BIT_COUNT' (T_STRING) in C:\xampp\htdocs\script.php on line 4.But never mind,i have found a alternate solution for this.Thanx. –  Aug 22 '14 at 08:29
  • Good to hear that:) Please put the solution here, so that it will be helpful for others. – arunrc Aug 22 '14 at 08:58
-1

Answer is strlen

$bit = "001011";
$count = strlen($bit);
echo $count;

OUTPUT : 6

Demo

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • 1
    It is ok,but how to compare both as 001011 ^ BINARY 001111 of different bit in both??? –  Aug 22 '14 at 06:59