是否可以判断哪个数组元素比较结果为 true?

发布于 2024-12-23 14:08:58 字数 409 浏览 1 评论 0原文

我有一个三维数组,我将其用作位表

   char bit_table_[X][Y][Z];

X 不大于 20,但 Y 和 Z 将非常大。 每个 X 的 Y 和 Z 的内容将按如下方式并行比较(这里将使用一些哈希函数计算 Y 和 Z 的实际值)。 我的问题是;我不知道是否有任何方法可以判断 if 语句的条件检查中哪个 X 为真

if (((bit_table_[0][i][bit_index / bits_per_char]|
bit_table_[1][i][bit_index / bits_per_char])& bit_mask[bit]) != bit_mask[bit])
     return true;

,或者是否有其他方法可以做到这一点?

i have a three dimensional array which i am using as a bit table

   char bit_table_[X][Y][Z];

X is not larger than 20 but Y and Z will be very large.
the contents of every X's Y and Z will be compared parallely as follows (here real values of Y and Z will be computed using some hash functions).
My problem is; I don't know if there is any way at all to tell as to which of the X's give true in the condition checking of the if statment

if (((bit_table_[0][i][bit_index / bits_per_char]|
bit_table_[1][i][bit_index / bits_per_char])& bit_mask[bit]) != bit_mask[bit])
     return true;

or is there anyother way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋叶绚丽 2024-12-30 14:08:58

您必须单独测试它们才能知道哪一个结果是正确的。下面是一个使用逻辑“或”而不是按位“或”来确定哪个结果为 true 的示例。

bool x1 = false, x2 = false;

if(
   (x1 = (bit_table_[0][i][bit_index/bits_per_char] & bit_mask[bit]) != bit_mask[bit]) ||
   (x2 = (bit_table_[1][i][bit_index/bits_per_char] & bit_mask[bit]) != bit_mask[bit])
  )
{
    //Check x1 and x2 here
    return true;
}

编辑:

要扩展前面的示例,并满足原始帖子的结果,您还可以检查两者的组合是否是它通过的原因,如下所示:

bool x1 = false, x2 = false, both = false;
size_t zindex = bit_index/bits_per_char;
if(
   (x1 = (bit_table_[0][i][zindex] & bit_mask[bit]) != bit_mask[bit]) ||
   (x2 = (bit_table_[1][i][zindex] & bit_mask[bit]) != bit_mask[bit]) ||
   (both = ((bit_table_[0][i][zindex] | bit_table_[1][i][zindex]) &
            bit_mask[bit]) != bit_mask[bit])
  )
{
    //Check x1 and x2 here
    //If both is true then neither x1 or x2 resulted in true alone

    return true;
}

You have to test them individually to know which one resulted in true. Here is an example using a logical rather than bitwise OR to determine which one resulted in true.

bool x1 = false, x2 = false;

if(
   (x1 = (bit_table_[0][i][bit_index/bits_per_char] & bit_mask[bit]) != bit_mask[bit]) ||
   (x2 = (bit_table_[1][i][bit_index/bits_per_char] & bit_mask[bit]) != bit_mask[bit])
  )
{
    //Check x1 and x2 here
    return true;
}

Edit:

To expand on the previous example, and to satisfy the results of your original post you could also check if the combination of both is the reason it passes like so:

bool x1 = false, x2 = false, both = false;
size_t zindex = bit_index/bits_per_char;
if(
   (x1 = (bit_table_[0][i][zindex] & bit_mask[bit]) != bit_mask[bit]) ||
   (x2 = (bit_table_[1][i][zindex] & bit_mask[bit]) != bit_mask[bit]) ||
   (both = ((bit_table_[0][i][zindex] | bit_table_[1][i][zindex]) &
            bit_mask[bit]) != bit_mask[bit])
  )
{
    //Check x1 and x2 here
    //If both is true then neither x1 or x2 resulted in true alone

    return true;
}
顾忌 2024-12-30 14:08:58

你的问题的解决方案很简单:仅当 x=0 和 x=1 的数组内容具有相同的值时,if 语句才为 true:0。假设数组仅包含二进制值(0 或 1)
if ( (0 | 0 )& 1 != 1) 为 true,bit_array 或 bit_mask 的所有其他情况都使该语句为 false!


如果数组可以包含 0 或 1 以外的其他值,则您不能说数组的内容对于 x=0 或 x=1 使语句成立。这是它们两者的关联:

简单的例子,如果您在条件为真时编写 if((0x0F | 0xF0) == 0) ,则 0x0F 或 0xF0 都不会使其为真!正是两者的关联才使条件成立。

这与您的 if 语句中的内容相同。

the solutionof your question is simple : the if statement is true only if the array content for x=0 and x=1 have the same value : 0. assumin that the array contain only binary values (0 or 1)
if ( (0 | 0 )& 1 ! = 1) is true all other cases for bit_array or bit_mask make the statement false !!


if the array can contain other values than 0 or 1, you can neither say that the content of the array for x=0 or for x=1 that makes the statement true. it is the association of the both of them :

simple example if you write if((0x0F | 0xF0) == 0) when the condition is true, neither 0x0F or 0xF0 makes it true ! it is the association of the two that make the condition true.

it is the same thing in your if statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文