5

我有两个 64 位整数xy. 它们每个代表5个短无符号整数:前10位代表第一个整数,接下来的13位代表第二个整数,接下来的16位代表第三个整数,接下来的14位代表第4个整数,其余位代表第 5 个整数。

x0, x1, x2, x3,x4是构成 的 5 个短整数x。让y0, y1, y2, y3,y4是构成 的 5 个短整数y。我需要知道x0 < y0AND x1 < y1AND AND x2 < y2AND x3 < y3AND x4 < y4

我认为最简单的解决方案是转移:

bool allLess(std::size_t x, std::size_t y)
{
  if(x >= y) return 0;
  int shift[] = {10, 13, 16, 14};
  for(int i = 0; i < 4; ++i)
  {
    x <<= shift[i];
    y <<= shift[i];
    if(x >= y) return 0;
  }
  return 1;
}

我知道有很多按位体操。有更快的解决方案吗?

4

2 回答 2

2

这并没有真正回答所提出的问题,而是解决了一个非常相似的问题:(如果可以重新组织实际问题,例如 OP,这可能会有所帮助)

如果整数没有紧密包装(即,如果每个“字段”之间和 MSB 端有一个零填充位),并且您想知道<=而不是<,我认为您可以只减去数字并检查是否有任何填充位发生了变化。(即。(y - x) & PADDING_MASK

于 2018-06-03T05:18:08.103 回答
0

您可以使用位域

#include <iostream>
#include <string.h>
#include <stdint.h>

struct CombInt64 {
        CombInt64(uint64_t x) {
                memcpy(this, &x, sizeof(uint64_t));
        }

        bool operator < (const CombInt64& other) const {
                std::cout << "Debug: self.a: " << a << " other.a: " << other.a << std::endl;
                std::cout << "Debug: self.b: " << b << " other.b: " << other.b << std::endl;
                std::cout << "Debug: self.c: " << c << " other.c: " << other.c << std::endl;
                std::cout << "Debug: self.d: " << d << " other.d: " << other.d << std::endl;
                std::cout << "Debug: self.e: " << e << " other.e: " << other.e << std::endl;

                return a < other.a && b < other.b && c < other.c && d < other.d && e < other.e;
        }
#if __BYTE_ORDER == __LITTLE_ENDIAN
        uint64_t a:10;
        uint64_t b:13;
        uint64_t c:16;
        uint64_t d:14;
        uint64_t e:11;
#elif __BYTE_ORDER == __BIG_ENDIAN
        uint64_t e:11;
        uint64_t d:14;
        uint64_t c:16;
        uint64_t b:13;
        uint64_t a:10;
#endif
};

bool allLess(uint64_t x, uint64_t y) {
        return CombInt64(x) < CombInt64(y);
}

int main(void) {
        std::cout << allLess(123, 45) << std::endl;
}

输出

[root@localhost tmp]# ./a.out
Debug: self.a: 123 other.a: 45
Debug: self.b: 0 other.b: 0
Debug: self.c: 0 other.c: 0
Debug: self.d: 0 other.d: 0
Debug: self.e: 0 other.e: 0
0

没有完全测试!!!

于 2020-01-09T02:48:14.373 回答