在下列代码的横线处填写( ),可以使得输出不是 “31”。
#include <iostream>
using namespace std;
int main() {
int array[5] = {1, 2, 4, 8, 16};
int res = 0;
for (int i = 0; i < 5; i++)
________; // 在此处填写代码
cout << res << endl;
return 0;
}看到 res=0 且要避开加法/异或/位或的累加效果,优先想位与的清零特性。
易把&误作&&,或以为0&任何数永远为0而忽略循环中res持续变为0的累积效应。
考点:位运算|数组遍历|初值敏感度
只有位与运算能让结果偏离31。