Comparing binary values in MySQL

喜你入骨 提交于 2020-01-11 09:25:11

问题


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.


回答1:


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



回答2:


SELECT BIT_COUNT(b'001011' ^ b'001111');



回答3:


It's converting the numbers 1011 and 1111 (base 10) to binary and doing the comparison. If you did:

SELECT BIT_COUNT(11 ^ 15)

It'd work.



来源:https://stackoverflow.com/questions/7111728/comparing-binary-values-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!