用于计算的“|”或“&”与 IF 语句内部的区别
Difference between " | " or " & " for calculations and inside IF statement
我正在尝试在 IF(或 SWITCH)语句中使用 |
来比较一个变量是否等于一个数字或另一个。
但我发现(在下面的代码中描述为示例)使用 |
运算符对我想比较的两个数字与我将 ||
用于两次比较的结果相同。但是,如果我声明另一个使用 |
对这两个数字进行或运算的变量,则 if 语句将不会执行:
(这“几乎”是完整代码)
using namespace std;
short n1 = 5, n2 = 3, n3, nResult;
n3 = 3; // used for two comparisons
nResult = n1 | n2; // used for the second comparison (IF statement)
bitset<16> n1_b(n1), n2_b(n2), n3_b(n3), nr_b(nResult); // I used bitset to check their binary value
if (n3 == nResult)
cout << "nResult YES";
else if (n3 == n1 | n2)
cout << "n1 | n2 YES";
/* cout << endl << n1_b << endl << n2_b << endl << n3_b << endl << endl << nr_b; */
输出总是n1 | n2 YES
。
为什么在 IF 语句中使用 m3 == n1 | n2
得到与使用 n3 == n1 || n3 == n2
相同的结果,为什么我之前的 ORed 不会执行?
这个if语句中的表达式
else if (n3 == n1 | n2)
等同于
else if ( ( n3 == n1 ) | n2)
子表达式 n3 == n1
( 3 == 5
) 产生的布尔值 false
被隐式转换为 0
.
所以
0 | n2
给出等于 n2
.
的非零值
因此表达式的结果是布尔值 true
..
至于这个if语句
if (n3 == nResult)
然后 nResult
计算得像 nResult = n1 | n2;
等于 7
不等于 n3
.
如果您想查看一个数字是否是“一组可能答案中的一个”,那么有几种方法。
例如,给定 n
和像 3, 5, 9
这样的集合,您可以使用 if
:
if (n == 3 || n == 5 || n == 9) {
// Matches
}
您可以使用 switch
:
switch (n) {
case 3:
case 5:
case 9:
// Matches
break;
}
您可以使用 std::vector
:
std::vector<int> matches = { 3, 5, 9 };
if (std::find(matches.begin(), matches.end(), n) != matches.end()) {
// Matches
}
您可以使用 std::set
:
std::set<int> matches = { 3, 5, 9 };
if (matches.find(n) != matches.end()) {
// Matches
}
您可以使用位图索引:
std::bitset<10> matches = "0001010001";
if (matches[n]) {
// Matches
}
你不能做的是使用 |
运算符将数字拼在一起。
我正在尝试在 IF(或 SWITCH)语句中使用 |
来比较一个变量是否等于一个数字或另一个。
但我发现(在下面的代码中描述为示例)使用 |
运算符对我想比较的两个数字与我将 ||
用于两次比较的结果相同。但是,如果我声明另一个使用 |
对这两个数字进行或运算的变量,则 if 语句将不会执行:
(这“几乎”是完整代码)
using namespace std;
short n1 = 5, n2 = 3, n3, nResult;
n3 = 3; // used for two comparisons
nResult = n1 | n2; // used for the second comparison (IF statement)
bitset<16> n1_b(n1), n2_b(n2), n3_b(n3), nr_b(nResult); // I used bitset to check their binary value
if (n3 == nResult)
cout << "nResult YES";
else if (n3 == n1 | n2)
cout << "n1 | n2 YES";
/* cout << endl << n1_b << endl << n2_b << endl << n3_b << endl << endl << nr_b; */
输出总是n1 | n2 YES
。
为什么在 IF 语句中使用 m3 == n1 | n2
得到与使用 n3 == n1 || n3 == n2
相同的结果,为什么我之前的 ORed 不会执行?
这个if语句中的表达式
else if (n3 == n1 | n2)
等同于
else if ( ( n3 == n1 ) | n2)
子表达式 n3 == n1
( 3 == 5
) 产生的布尔值 false
被隐式转换为 0
.
所以
0 | n2
给出等于 n2
.
因此表达式的结果是布尔值 true
..
至于这个if语句
if (n3 == nResult)
然后 nResult
计算得像 nResult = n1 | n2;
等于 7
不等于 n3
.
如果您想查看一个数字是否是“一组可能答案中的一个”,那么有几种方法。
例如,给定 n
和像 3, 5, 9
这样的集合,您可以使用 if
:
if (n == 3 || n == 5 || n == 9) {
// Matches
}
您可以使用 switch
:
switch (n) {
case 3:
case 5:
case 9:
// Matches
break;
}
您可以使用 std::vector
:
std::vector<int> matches = { 3, 5, 9 };
if (std::find(matches.begin(), matches.end(), n) != matches.end()) {
// Matches
}
您可以使用 std::set
:
std::set<int> matches = { 3, 5, 9 };
if (matches.find(n) != matches.end()) {
// Matches
}
您可以使用位图索引:
std::bitset<10> matches = "0001010001";
if (matches[n]) {
// Matches
}
你不能做的是使用 |
运算符将数字拼在一起。