1

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
  • 1
    Storing binary data in a VARCHAR seems 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)? Commented Oct 23, 2019 at 11:00

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.