按段比较64位整数 [英] Compare 64-bit integers by segments

查看:132
本文介绍了按段比较64位整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个64位整数xy.它们每个代表5个短无符号整数:前10位代表第一个整数,后13位代表第二个整数,后16位代表第3个整数,后14位代表第4个整数,其余位代表第五个整数.

I have two 64-bit integers x and y. Each of them represents 5 short unsigned integers: the first 10 bits represent the first integer, the next 13 bits represent the 2nd integer, the next 16 bits represent the 3rd integer, the next 14 bits represent the 4th integer, and the rest bits represent the 5th integer.

x0x1x2x3x4是构成x的5个短整数.假设y0y1y2y3y4是构成y的5个短整数.我需要知道x0 < y0x1 < y1x2 < y2x3 < y3x4 < y4.

Let x0, x1, x2, x3, x4 be the 5 short integers that constitute x. Let y0, y1, y2, y3, y4 be the 5 short integers that constitute y. I need to know if x0 < y0 AND x1 < y1 AND x2 < y2 AND x3 < y3 AND x4 < y4.

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

I assume the simplest solution is to shift:

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;
}

我知道有很多按位体操.任何更快的解决方案?

I know there are lots of bitwise gymnastics. Any faster solution?

推荐答案

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

This doesn't really answer the question as posed, but solves a very similar problem: (Which might help if one is in the position to reorganize the actual problem, like the OP)

如果整数不是紧密包装的(即,如果每个字段"之间以及MSB末尾只有一个零填充位),并且您想知道<=而不是<,我认为您也许可以减去这些数字并检查是否有任何填充位更改. (即(y - x) & PADDING_MASK)

If the integers weren't tightly packed (ie. if there was a single zero bit of padding between each "field", and at the MSB end), and you wanted to know <= instead of <, I think you might be able to just subtract the numbers and check if any of the padding bits changed. (ie. (y - x) & PADDING_MASK)

这篇关于按段比较64位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆