I have a table so this table have one column which name is bit_column. And type of this column is varchar(64). this column only contain zeros and ones. (Ex: '1010100010011110111011101.....'). I want to XOR this column with given string (this string also contain zeros and ones). For ex: BIT_COUNT(b'01001' ^ b'01101') this return 1. So how can i do it on table column?
1 Answer
Convert the column value and the given string to decimal integer number with CONV() and CAST() and apply the bitwise XOR operator ^.
Then convert the result to binary and left pad it with zeroes :
SET @s = '0011001100110011001100110011001100110011001100110011001100110011';
SELECT
LPAD(CONV(
CAST(CONV(bit_column, 2, 10) AS UNSIGNED INTEGER)
^
CAST(CONV(@s, 2, 10) AS UNSIGNED INTEGER),
10, 2
), 64, '0')
FROM tablename
See the demo.
VARCHARseems like a poor choice to me, and has given rise to the issue you are experiencing right now. Can you change the design to store it more apporpriately (and therefore allow bitwise operations)?