本页面包含课程例题、课后拓展5关、代码填空4关的完整答案与家长辅导建议。
请输入本讲访问密码:
课程辅导解析(家长端) · 含全部例题、拓展、代码填空答案
#include <iostream> using namespace std; int main() { int a = 12, b = 10; cout << (a & b) << endl; // 8 cout << (a | b) << endl; // 14 cout << (a ^ b) << endl; // 6 return 0; }
#include <iostream> #include <bitset> using namespace std; int main() { cout << bitset<4>(5) << endl; // 0101 cout << bitset<4>(7) << endl; // 0111 return 0; }
#include <iostream> #include <bitset> using namespace std; int main() { int n = 5; // 0101 if (n & (1 << 2)) cout << "bit2=1\n"; // 是 n |= (1 << 1); // 0101 | 0010 = 0111 cout << bitset<4>(n); // 0111 return 0; }
#include <iostream> using namespace std; int main() { int a = 3, b = 9; a = a ^ b; // a=3^9 b = a ^ b; // b=3^9^9=3 a = a ^ b; // a=3^9^3=9 cout << a << " " << b << endl; // 9 3 return 0; }
#include <iostream> using namespace std; int main() { int n; cin >> n; int cnt = 0; while (n > 0) { if (n & 1) cnt++; // ★ 最低位是1就计数 n = n >> 1; // ★ 右移一位 } cout << cnt << endl; return 0; }
#include <iostream> using namespace std; int main() { int n; cin >> n; if (n & 1) cout << "odd" << endl; else cout << "even" << endl; return 0; }
#include <iostream> using namespace std; int main() { int n = 5; n = n | (1 << 1); // 0101 | 0010 = 0111 cout << n << endl; // 7 return 0; }
#include <iostream> using namespace std; int main() { int a = 3, b = 9; a = a ^ b; b = a ^ b; a = a ^ b; cout << a << " " << b << endl; // 9 3 return 0; }
#include <iostream> using namespace std; int main() { int n; cin >> n; int cnt = 0; while (n > 0) { if (n & 1) cnt++; n = n >> 1; } cout << cnt << endl; return 0; }