开发者

Comparing binary values in MySQL

开发者 https://www.devze.com 2023-03-29 16:06 出处:网络
Say you have two binary values 001011 001111 How can you get the number of different bits in MySQL? I tried

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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消