【问题标题】:Redis: Computing Hamming DistanceRedis:计算汉明距离
【发布时间】:2019-04-23 11:52:51
【问题描述】:

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

【问题讨论】:

    标签: redis


    【解决方案1】:

    是的,您可以使用 BITOPBITCOUNT 命令来做到这一点。

    为了计算汉明距离,您可以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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-31
      • 2014-09-25
      • 2016-06-05
      • 2022-01-16
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      相关资源
      最近更新 更多