【发布时间】:2019-04-23 11:52:51
【问题描述】:
是否可以使用 Redis 命令计算 {0, 1}^n, s.a., https://redis.io/commands/bitfield 中的两个条目之间的汉明距离?
【问题讨论】:
标签: redis
是否可以使用 Redis 命令计算 {0, 1}^n, s.a., https://redis.io/commands/bitfield 中的两个条目之间的汉明距离?
【问题讨论】:
标签: redis
是的,您可以使用 BITOP 和 BITCOUNT 命令来做到这一点。
为了计算汉明距离,您可以XOR两个给定的条目,并计算结果中1s的数量。
// 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
【讨论】: