In case you’ve forgotten:
Bit Operation | Symbol |
---|---|
AND: this is a single ampersand: | & |
OR: this is a single vertical bar: | | |
XOR: this is a single carat: | ^ |
NOT: this is a single tilde: | ~ |
Left-shift: this is two less-than signs: | « |
Right-shift: this is two greater-than signs: | » |
For i between 1 and 6, Question i is to tell me what gets printed on line i of the program to the right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a, b, c, d, e;
a = 32;
b = 10;
c = 0x3b53a;
d = 0x3b93a;
e = 0x8372a;
printf("%d\n", 5 & 9); // Question 1
printf("%d\n", 3 | 12); // Question 2
printf("0x%x\n", a + b); // Question 3
printf("0x%x\n", c ^ d); // Question 4
printf("0x%x\n", e << 8); // Question 5
printf("0x%x\n", e >> 4); // Question 6
return 0;
}
0101 -- 5 in binary 1001 -- 9 in binary ---- 0001 -- so the answer is 1
0011 -- 3 in binary 1101 -- 12 in binary ---- 1111 -- so the answer is 15
0x2a
.
3 b 5 3 a 3 b 9 3 a --------- 0 0 c 0 0 -- the answer is 0xc00
0x8372a00
.
0x8372
.
</pre>