在以下情况下 & 运算符会发生什么?
What happens due to & operator in the following case?
我知道,“&”作为按位运算符和运算符来获取变量的内存地址。
在这种情况下代码会发生什么?
res = res & (a[i]<[a[i+1]]);
如果是按位and,据我所知第二个条件也被检查,
但是如果我用 logical and 而不是它,它会不会还是一样呢?
由于第一部分是(比如说)false,第二部分被检查为 true,但 res 仍然是 false。
为此使用 logical and 是否相同(对于这种情况)?或者它在这种情况下有其他用途(& 运算符)?
int a[] {1,3,4,2};
int pos = 3;
bool res = true;
for(int i = 0; i < pos; i++)
res &= (a[i] < a[i + 1]);
(抱歉英语不好)
If it is bitwise and , as far as I know the second condition is also checked, but what if I used logical and instead of it , wouldn't it still be the same?
没有。布尔值和(写为 &&
或 and
)具有短路评估 - 如果左侧部分为假,则右侧部分根本不会被评估。这允许编写如下代码:
if( pointer != nullptr && pointer->value > 100 ) ...
如果不进行短路评估,此代码将有 UB。例如这段代码:
if( pointer != nullptr & pointer->value > 100 ) ...
当 pointer
等于 nullptr
时, 有未定义的行为
Would it be same (for this case) to use logical and for this? or it has some other use (& operator) for this case?
你不能,因为 C++ 中没有 &&=
运算符。你可以这样写:
res = res && (a[i] < a[i + 1]);
这也会造成短路,编译器甚至可能足够聪明来停止循环,尽管我怀疑并且无论如何都应该明确表达:
bool res = true;
for(int i = 0; res && i < pos; i++)
res = a[i] < a[i + 1];
它的作用相同,但更清洁、更高效。
无论如何,当你需要逻辑或布尔值时,你应该使用一个来明确你的意图并避免意外的惊喜。
除了短路问题,如果res == 2
那么:
res & 1
将 return 0
解释为 false
。
res && 1
将 return true
.
你的问题不清楚。
好的,让我们深入研究您的代码。
你给的代码很清楚。您正在按位执行 pos(3) 次。对于每个循环,您都将 a[i] 与 a[i+1] 进行比较。请注意,对于最后一个循环,我的意思是当变量 i 变为 3 时,i+1 将为 4。并且您的数组 a[] 没有 a[4]。它只有索引为 3 的最后一个元素。
因此对于按位和运算,res 的值不可预测,因为 a[4] 未定义。
现在让我们考虑一下逻辑与操作。 For logical and your expression inside for loop will once generate a false boolean value for a[i] < a[i+1] as your array a[] = {1,3,4,2}.这里 4>2 而不是 4<2。因此,它将生成错误的布尔值,并且您的整个响应都将是错误的,因为您知道逻辑 AND 如果其中一个操作数为假,则最终将为 0。
我想你已经明白了。
我知道,“&”作为按位运算符和运算符来获取变量的内存地址。 在这种情况下代码会发生什么? res = res & (a[i]<[a[i+1]]);
如果是按位and,据我所知第二个条件也被检查, 但是如果我用 logical and 而不是它,它会不会还是一样呢?
由于第一部分是(比如说)false,第二部分被检查为 true,但 res 仍然是 false。
为此使用 logical and 是否相同(对于这种情况)?或者它在这种情况下有其他用途(& 运算符)?
int a[] {1,3,4,2};
int pos = 3;
bool res = true;
for(int i = 0; i < pos; i++)
res &= (a[i] < a[i + 1]);
(抱歉英语不好)
If it is bitwise and , as far as I know the second condition is also checked, but what if I used logical and instead of it , wouldn't it still be the same?
没有。布尔值和(写为 &&
或 and
)具有短路评估 - 如果左侧部分为假,则右侧部分根本不会被评估。这允许编写如下代码:
if( pointer != nullptr && pointer->value > 100 ) ...
如果不进行短路评估,此代码将有 UB。例如这段代码:
if( pointer != nullptr & pointer->value > 100 ) ...
当 pointer
等于 nullptr
时,有未定义的行为
Would it be same (for this case) to use logical and for this? or it has some other use (& operator) for this case?
你不能,因为 C++ 中没有 &&=
运算符。你可以这样写:
res = res && (a[i] < a[i + 1]);
这也会造成短路,编译器甚至可能足够聪明来停止循环,尽管我怀疑并且无论如何都应该明确表达:
bool res = true;
for(int i = 0; res && i < pos; i++)
res = a[i] < a[i + 1];
它的作用相同,但更清洁、更高效。
无论如何,当你需要逻辑或布尔值时,你应该使用一个来明确你的意图并避免意外的惊喜。
除了短路问题,如果res == 2
那么:
res & 1
将 return 0
解释为 false
。
res && 1
将 return true
.
你的问题不清楚。 好的,让我们深入研究您的代码。
你给的代码很清楚。您正在按位执行 pos(3) 次。对于每个循环,您都将 a[i] 与 a[i+1] 进行比较。请注意,对于最后一个循环,我的意思是当变量 i 变为 3 时,i+1 将为 4。并且您的数组 a[] 没有 a[4]。它只有索引为 3 的最后一个元素。
因此对于按位和运算,res 的值不可预测,因为 a[4] 未定义。
现在让我们考虑一下逻辑与操作。 For logical and your expression inside for loop will once generate a false boolean value for a[i] < a[i+1] as your array a[] = {1,3,4,2}.这里 4>2 而不是 4<2。因此,它将生成错误的布尔值,并且您的整个响应都将是错误的,因为您知道逻辑 AND 如果其中一个操作数为假,则最终将为 0。
我想你已经明白了。