Python Questions and Answers – Bitwise – 2

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Bitwise – 2”.

1. It is not possible for the two’s complement value to be equal to the original value in any case.
a) True
b) False
View Answer

Answer: b
Explanation: In most cases, the two’s complement of a binary number is different from the original value. However, there is a special case in which the two’s complement is equal to the original value. For example, in an 8-bit system, the binary number 10000000 represents -128. Taking the two’s complement of this number results in the same binary value: 10000000. This happens because -128 is the minimum value that can be represented in 8-bit two’s complement format, and it does not have a positive counterpart. Hence, the statement is false.

2. The one’s complement of 110010101 is:
a) 001101010
b) 110010101
c) 001101011
d) 110010100
View Answer

Answer: a
Explanation: The one’s complement of a value is obtained by simply changing all the 1’s to 0’s and all the 0’s to 1’s. Hence the one’s complement of 110010101 is 001101010.

3. Bitwise _________ gives 1 if either of the bits is 1 and 0 when both of the bits are 1.
a) OR
b) AND
c) XOR
d) NOT
View Answer

Answer: c
Explanation: Bitwise XOR gives 1 if the two bits are different, and 0 if they are the same. So, it returns 1 when one bit is 1 and the other is 0.
advertisement

4. What will be the output of the following Python expression?

print(4^12)

a) 2
b) 4
c) 8
d) 12
View Answer

Answer: c
Explanation: ^ is the XOR operator. The binary form of 4 is 0100 and that of 12 is 1100. Therefore, 0100^1100 is 1000, which is equal to 8.
Free 30-Day Java Certification Bootcamp is Live. Join Now!

5. Any odd number on being AND-ed with ________ always gives 1. Hint: Any even number on being AND-ed with this value always gives 0.
a) 10
b) 2
c) 1
d) 0
View Answer

Answer: c
Explanation: Any odd number on being AND-ed with 1 always gives 1. Any even number on being AND-ed with this value always gives 0.

6. What will be the value of the following Python expression?

print(bin(10-2)+bin(12^4))

a) 0b10000
b) 0b10001000
c) 0b1000b1000
d) 0b10000b1000
View Answer

Answer: d
Explanation: The value of the expression bin(10 – 2) + bin(12 ^ 4) is ‘0b1000b1000’ because bin(10 – 2) results in ‘0b1000’ and bin(12 ^ 4) also results in ‘0b1000’, and their concatenation gives ‘0b1000b1000’.

7. Which of the following expressions can be used to multiply a given number ‘a’ by 4?
a) a<<2
b) a<<4
c) a>>2
d) a>>4
View Answer

Answer: a
Explanation: Let us consider an example wherein a=2. The binary form of 2 is 0010. When we left shift this value by 2, we get 1000, the value of which is 8. Hence if we want to multiply a given number ‘a’ by 4, we can use the expression: a<<2.
advertisement

8. What will be the output of the following Python code if a=10 and b =20?

a=10
b=20
a=a^b
b=a^b
a=a^b
print(a,b)

a) 10 20
b) 10 10
c) 20 10
d) 20 20
View Answer

Answer: c
Explanation: The code shown above is used to swap the contents of two memory locations using bitwise X0R operator. Hence the output of the code shown above is: 20 10.

9. What is the two’s complement of -44?
a) 1011011
b) 11010100
c) 11101011
d) 10110011
View Answer

Answer: b
Explanation: The binary form of -44 is 00101100. The one’s complement of this value is 11010011. On adding one to this we get: 11010100 (two’s complement).

10. What will be the output of the following Python expression?

print(~100)

a) 101
b) -101
c) 100
d) -100
View Answer

Answer: b
Explanation: The expression ~100 uses the bitwise NOT operator, which inverts all the bits of the number. In Python, this is equivalent to -(100 + 1), which results in -101. Therefore, the output of ~100 is -101.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.