是否可以判断哪个数组元素比较结果为 true?
我有一个三维数组,我将其用作位表
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须单独测试它们才能知道哪一个结果是正确的。下面是一个使用逻辑“或”而不是按位“或”来确定哪个结果为 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.
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:
你的问题的解决方案很简单:仅当 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.