Bitwise Operations Flashcards
7 cards from real PCEP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Bitwise Operations flashcards as text
What is the result of the bitwise AND operation `10 & 12`?
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.
What is the output of the bitwise OR operation `9 | 5`?
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.
What is the result of the bitwise XOR operation `13 ^ 7`?
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.
What is the output of the bitwise NOT operation `~5`?
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.
What is the result of the bitwise left shift operation `6 << 2`?
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.
What is the output of the bitwise right shift operation `20 >> 1`?
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.
What is the result of the following combination of bitwise operations? `print((7 | 2) & 10)`
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.