Free PCEP Bitwise Operations Questions and Answers 1 — Questions and Answers
Question 1: What is the result of the bitwise AND operation `10 & 12`?
- 10
- 12
- 8 (Correct answer)
- 2
Correct answer: 8
The binary representation of 10 is `1010` and for 12 is `1100`. The bitwise AND (`&`) operation compares each bit and returns 1 only if both bits are 1. Comparing `1010` and `1100` results in `1000`, which is the decimal number 8.
Question 2: What is the output of the bitwise OR operation `9 | 5`?
- 12
- 13 (Correct answer)
- 4
- 1
Correct answer: 13
The binary representation of 9 is `1001` and for 5 is `0101`. The bitwise OR (`|`) operation compares each bit and returns 1 if at least one of the bits is 1. Comparing `1001` and `0101` results in `1101`, which is the decimal number 13.
Question 3: What is the result of the bitwise XOR operation `13 ^ 7`?
- 15
- 6
- 10 (Correct answer)
- 8
Correct answer: 10
The binary representation of 13 is `1101` and for 7 is `0111`. The bitwise XOR (`^`) operation compares each bit and returns 1 only if the bits are different. Comparing `1101` and `0111` results in `1010`, which is the decimal number 10.
Question 4: What is the output of the bitwise NOT operation `~5`?
- 5
- -5
- 6
- -6 (Correct answer)
Correct answer: -6
The bitwise NOT (`~`) operator inverts all the bits of its operand. In Python, for an integer `x`, `~x` is equivalent to `-(x + 1)`. Therefore, `~5` is `-(5 + 1)`, which equals -6.
Question 5: What is the result of the bitwise left shift operation `6 << 2`?
- 12
- 24 (Correct answer)
- 3
- 1
Correct answer: 24
The binary representation of 6 is `110`. The left shift (`<<`) operator shifts the bits of the number to the left by the specified number of places, filling the new positions on the right with zeros. Shifting `110` left by 2 places results in `11000`, which is the decimal number 24.
Question 6: What is the output of the bitwise right shift operation `20 >> 1`?
- 40
- 5
- 10 (Correct answer)
- 21
Correct answer: 10
The binary representation of 20 is `10100`. The right shift (`>>`) operator shifts the bits of the number to the right by the specified number of places, discarding the bits that are shifted off. Shifting `10100` right by 1 place results in `1010`, which is the decimal number 10.
Question 7: What is the result of the following combination of bitwise operations? `print((7 | 2) & 10)`
- 2 (Correct answer)
- 7
- 9
- 10
Correct answer: 2
Following operator precedence, the expression in the parentheses is evaluated first. `7 | 2` (binary `0111 | 0010`) results in `0111`, which is 7. The expression then becomes `7 & 10`. `7` in binary is `0111` and `10` is `1010`. The result of `0111 & 1010` is `0010`, which is the decimal number 2.
What is the result of the bitwise AND operation `10 & 12`?