是否可以使用Redis命令计算{0,1} ^ n,s.a.,https://redis.io/commands/bitfield中两个条目之间的汉明距离?
答案 0 :(得分:2)
是的,您可以使用BITOP
和BITCOUNT
命令来实现。
要计算汉明距离,您可以 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