Redis:计算汉明距离

时间:2018-11-20 23:24:47

标签: redis

是否可以使用Redis命令计算{0,1} ^ n,s.a.,https://redis.io/commands/bitfield中两个条目之间的汉明距离?

1 个答案:

答案 0 :(得分:2)

是的,您可以使用BITOPBITCOUNT命令来实现。

要计算汉明距离,您可以 XOR 两个给定的条目,并计算结果中1的数量。

// The first entry: 10000001
SETBIT k1 0 1
SETBIT k1 7 1
// The second entry: 00000010
SETBIT k2 6 1

// first entry XOR second entry: 10000011
BITOP XOR result k1 k2
// count the number of 1s in the result, i.e. the Hamming Distance between the two entries: 3
BITCOUNT result
相关问题