布尔递归函数中的真/假优先级

发布于 2025-01-02 09:41:35 字数 1217 浏览 1 评论 0原文

我已经阅读了以前的帖子,并且学到了一些东西,但想验证一些循环是如何工作的。在阅读中,我是否正确理解“真”比“假”具有更高的优先级?例如:

/.../
return (true || false);

会返回“true”(无论顺序如何)?

如果我有一个布尔递归函数,它调用自身的 3 个变体...我所需要的只是一个版本返回 true,以便整个函数返回 true,对吗?下面的函数创建它的堆栈帧,然后返回调用创建另外 3 个堆栈帧并运行调用,然后如果一个返回 true,则整个函数返回 true,因为 true 优先于 false...这个假设正确吗?

即:

/* This function is taking a given weight and seeing if it can be offset by available 
 * weights. Depending on what weights are available, the weights can be directly opposed
 * to "weight" (opposite side of scale) or added to... The recursive calls will either all 
 * return false, all return true, or a variation thereof.  All that I can is that if one 
 * branch returns true, the overall function returns true...
*/

bool CanMeasure(int weight, std::vector<int> &availableWeights, int index = 0)
{
    /.../
    // the below can return all true, all false, or some variation thereof...
    return (CanMeasure(weight + availableWeights[index], availableWeights, index + 1) ||
            CanMeasure(weight - availableWeights[index], availableWeights, index + 1) ||
            CanMeasure(weight, availableWeights, index + 1));
}

谢谢大家!

I've done some reading of previous posts and I've learned some things but want to verify how some loops are working. In reading, am I right in understanding that "true" has higher precedence than "false"? For instance:

/.../
return (true || false);

will return "true" (regardless of order)?

If I have a boolean recursive function that calls 3 variations of itself...all I need is for one version to return true for the whole funct to return true, correct? The function below creates it's stack frame, then the return call creates 3 more stack frames and runs through the calls, then if one returns true the whole funct returns true because true has precedence over false... Is this assumption correct?

Ie:

/* This function is taking a given weight and seeing if it can be offset by available 
 * weights. Depending on what weights are available, the weights can be directly opposed
 * to "weight" (opposite side of scale) or added to... The recursive calls will either all 
 * return false, all return true, or a variation thereof.  All that I can is that if one 
 * branch returns true, the overall function returns true...
*/

bool CanMeasure(int weight, std::vector<int> &availableWeights, int index = 0)
{
    /.../
    // the below can return all true, all false, or some variation thereof...
    return (CanMeasure(weight + availableWeights[index], availableWeights, index + 1) ||
            CanMeasure(weight - availableWeights[index], availableWeights, index + 1) ||
            CanMeasure(weight, availableWeights, index + 1));
}

Thanks guys!

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

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

发布评论

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

评论(6

温折酒 2025-01-09 09:41:35

truefalse 是值,而不是运算符 - 因此它们没有优先级。

但是,如果结果已知,&&|| 运算符确实可以简化计算过程。因此,如果左侧表达式产生 true 并且您应用 ||,则不会计算右侧表达式;这同样适用于 false&&

true and false are values, not operators - as such they have no precedence.

However, the && and || operators do shortcut the evaluation if the result is known; so if the lefthand expression yields true and you apply ||, the righthand expression will not be evaluated; the same applies to false and &&.

朱染 2025-01-09 09:41:35

return (true || false); 不评估 false 位的原因是短路布尔评估。请参阅 C++ 中的短路评估是否有保证和 Java 中一样吗?。与 && 类似,(false && true) 不会评估“true”。

The reason why return (true || false); doesn't evaluate the false bit is because of short circuit boolean evaluation. See Is Short Circuit Evaluation guaranteed In C++ as it is in Java?. Similary with &&, (false && true) won't evaluate 'true'.

找回味觉 2025-01-09 09:41:35

是的(无论顺序如何,它都会返回true)。 or 中的条件从左到右进行评估,当遇到第一个 true 时,整个条件返回 true

在您的示例中:

return (CanMeasure(weight + availableWeights[index], availableWeights, index + 1) ||
        CanMeasure(weight - availableWeights[index], availableWeights, index + 1) ||
        CanMeasure(weight, availableWeights, index + 1));

并非所有条件都需要评估。如果第一个函数的计算结果为 true,则其他函数将不会执行,并且该函数将仅返回 true

这就是所谓的短路。

让我们看一下一些反汇编代码:

   if ( foo() || goo() )
0041152E  call        foo (41111Dh) 
00411533  movzx       eax,al 
00411536  test        eax,eax 
00411538  jne         wmain+36h (411546h) 
0041153A  call        goo (4111A9h) 
0041153F  movzx       eax,al 
00411542  test        eax,eax 
00411544  je          wmain+49h (411559h) 

在这个例子中,foo()goo() 都是返回 bool 的函数。

该指令

00411538  jne         wmain+36h (411546h) 

告诉运行时如果 foo() 计算结果为 true 则跳出条件。

此代码未经过优化,因此它不是优化功能。

Yes (it will return true regardless of order). The conditions in the or are evaluated from left to right, and when the first true is stumbled upon, the whole condition returns true.

In your example:

return (CanMeasure(weight + availableWeights[index], availableWeights, index + 1) ||
        CanMeasure(weight - availableWeights[index], availableWeights, index + 1) ||
        CanMeasure(weight, availableWeights, index + 1));

not all conditions are necesarily evaluated. If the first one evaluates to true, the others will not execute, and the function will just return true.

It's called short-circuiting.

Let's take a look at some dissasembled code:

   if ( foo() || goo() )
0041152E  call        foo (41111Dh) 
00411533  movzx       eax,al 
00411536  test        eax,eax 
00411538  jne         wmain+36h (411546h) 
0041153A  call        goo (4111A9h) 
0041153F  movzx       eax,al 
00411542  test        eax,eax 
00411544  je          wmain+49h (411559h) 

In this example, foo() and goo() are both functions returning bool.

The instruction

00411538  jne         wmain+36h (411546h) 

tells the runtime to jump out of the conditional if foo() evaluated to true.

This code is not optimized, so it's not an optimization feature.

沫离伤花 2025-01-09 09:41:35

|| 的规则运算:

true || true => true
true || false => true
false || true => true
false || false => false

如果其中一个操作数为真,则无论其位置如何,结果均为真。
即使你有类似 false || 的东西假||假...假|| true 结果将为 true。

The rules for || operation:

true || true => true
true || false => true
false || true => true
false || false => false

If one of the operands are true, no matter of it's position, the result will be true.
Even if you have something like false || false || false ... false || true the result will be true.

少钕鈤記 2025-01-09 09:41:35

是的,一个返回值(true)足以确定结果。
这是通过短路评估来定义的。

Yes, one return value, which is true does suffice to determine the result.
This is defined by short circuit evaluation.

http://en.wikipedia.org/wiki/Short-circuit_evaluation

回心转意 2025-01-09 09:41:35

我不认为这个问题是关于短路评估的。而“true 的优先级高于 false”是没有意义的。 True 是一个值,而不是一个操作。

true || 的原因false 返回 true 只是逻辑,与编程无关。逻辑运算 AND 和 OR 不关心其输入的顺序。他们只关心“有多少是真的”。特别是,如果两个都为真,则AND返回真,而如果至少一个为真,则OR返回真。

短路评估是另一个主题(尽管它可能对您避免某些函数调用仍然有用)。

I don't think this question is about short circuit evaluation. And "true has higher precedence than false" does not make sense. True is a value, not an operation.

The reason why true || false returns true is just logic, and has nothing to do here with programming. The logic operation AND and OR don't care about the order of their inputs. They just care about "how many are true". In particular, AND returns true if both are true, while OR returns true if at least one is true.

The short circuit evaluation is another topic (which might still be useful to you to avoid some function call though).

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