Practice mode Python

What is the output of the following Python code? print(5 & 3, 5 | 3, 5 ^ 3)

Correct answer C. 1 7 6

Explanation

In binary 5 is 101 and 3 is 011. Bitwise AND keeps bits set in both (001 = 1), OR keeps bits set in either (111 = 7), and XOR keeps bits set in exactly one (110 = 6). A common slip is to treat ^ as exponentiation, which is written ** in Python.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.