Search code examples
mysqlbinarybitcount

Comparing binary values in MySQL


Say you have two binary values

001011 
001111

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

SELECT BIT_COUNT(BINARY  001011 ^ BINARY 001111)

This returns 6, while I need a solution that returns 1 in this example.


Solution

  • SELECT BIT_COUNT( CONV( '001011', 2, 10 ) ^ CONV( '001111', 2, 10 ) )