Bitwise comparison of Python characters

It is necessary to calculate the bit difference of characters in python.

I.e., roughly speaking, the character " a "and the character" b "will have a difference of 1, the characters" m " and " o " will have a difference of 2. etc.

Using arrays is not an option, any unicode characters can be compared.

Something like the Levenshtein distance, only between characters and the difference between characters will be equal to the difference of the sum of their bits.

Author: alexandre0sheva, 2015-04-08

3 answers

You have a strange wording. When calculating the bit sum difference, the difference between "a" and "b" will be zero:

"a" = 01100001
"b" = 01100010

In both cases, the sum is 3, and 3 - 3 = 0.

If we take the number of different bits, then there are two of them.

If we take the binary representation of the numbers corresponding to " a " and "b" as strings, then the Levenshtein distance will also be 2.

In general, the proposed method of calculating the distance is not clear, I can not guess how you got 1 succeeded.

In any case, if you need to count the number of single bits, then the easiest way in Python is this (I will immediately make a reservation that there are much more computationally efficient ways to do this):

bin(ord('a')).count('1')

If you need to get the number of bits that differ, then do it this way:

bin(ord('a') ^ ord('b')).count('1')

Here, the exclusive OR is first calculated (XOR), the value of which will have those digits that are single either in "a" or in "b", but not at the same time (that is, different digits), and then, as above, the number of single digits (that is, those very different digits) is counted.

It is, alas, difficult to say more without the correct formulation of the problem. Specify the task so that you can say something more specific.

 1
Author: Vadim Shender, 2015-04-08 09:45:18

You can do this:

print(ord('o') - ord('m'))
 0
Author: Avernial, 2015-04-08 09:02:53

Is it about the number of different bits? Then something like this

delta=ord('b') ^ ord('a')
bitCount=0
maxBit=16
for i in range(0,maxBit):
  mask = 1 << i
  if (delta & mask)!=0 :
    bitCount += 1

I can still be wrong about writing bit operations and range Although not-apparently, still not what you need...

 0
Author: user3476487, 2015-04-08 10:03:20