Python Questions and Answers – Bitwise – 1

Q 1. What will be the output of the following Python code snippet if x=1?

x<<2

A. 8
B. 1
C. 2
D. 4

Show Answer Answer:-D. 4
Explanation The binary form of 1 is 0001. The expression x<<2 implies we are performing bitwise left shift on x. This shift yields the value: 0100, which is the binary form of the number 4.

Q 2. What will be the output of the following Python expression?

bin(29)

A. ‘0b10111’
B. ‘0b11101’
C. ‘0b11111’
D. ‘0b11011’

Show Answer Answer:-B. ‘0b11101’
Explanation The binary form of the number 29 is 11101. Hence the output of this expression is ‘0b11101’.

Q 3. What will be the value of x in the following Python expression, if the result of that expression is 2?

x>>2

A. 8
B. 4
C. 2
D. 1

Show Answer Answer:-A. 8
Explanation When the value of x is equal to 8 (1000), then x>>2 (bitwise right shift) yields the value 0010, which is equal to 2. Hence the value of x is 8.

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

int(1011)?

A. 1011
B. 11
C. 13
D. 1101

Show Answer Answer:-A. 1011
Explanation The result of the expression shown will be 1011. This is because we have not specified the base in this expression. Hence it automatically takes the base as 10.

Q 5. To find the decimal value of 1111, that is 15, we can use the function:
A. int(1111,10)
B. int(‘1111’,10)
C. int(1111,2)
D. int(‘1111’,2)

Show Answer Answer:-D. int(‘1111’,2)
Explanation The expression int(‘1111’,2) gives the result 15. The expression int(‘1111’, 10) will give the result 1111.

Q 6. What will be the output of the following Python expression if x=15 and y=12?

x & y

A. b1101
B. 0b1101
C. 12
D. 1101

Show Answer Answer:-C. 12
Explanation The symbol ‘&’ represents bitwise AND. This gives 1 if both the bits are equal to 1, else it gives 0. The binary form of 15 is 1111 and that of 12 is 1100. Hence on performing the bitwise AND operation, we get 1100, which is equal to 12.

Q 7. Which of the following expressions results in an error?
A. int(1011)
B. int(‘1011’,23)
C. int(1011,2)
D. int(‘1011’)

Show Answer Answer:-C. int(1011,2)
Explanation The expression int(1011,2) results in an error. Had we written this expression as int(‘1011’,2), then there would not be an error.

Q 8. Which of the following represents the bitwise XOR operator?
A. &
B. ^
C. |
D. !

Show Answer Answer:-B. ^
Explanation The ^ operator represent bitwise XOR operation. &: bitwise AND, | : bitwise OR and ! represents bitwise NOT.

Q 9. What is the value of the following Python expression?

bin(0x8)

A. ‘0bx1000’
B. 8
C. 1000
D. ‘0b1000’

Show Answer Answer:-D. ‘0b1000’
Explanation The prefix 0x specifies that the value is hexadecimal in nature. When we convert this hexadecimal value to binary form, we get the result as: ‘0b1000’.

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

0x35 | 0x75

A. 115
B. 116
C. 117
D. 118

Show Answer Answer:-C. 117
Explanation The binary value of 0x35 is 110101 and that of 0x75 is 1110101. On OR-ing these two values we get the output as: 1110101, which is equal to 117. Hence the result of the above expression is 117.

Q 11. Which of the following is the Python Bitwise AND operator?

A. &

B. &&

C. and

D. !

Show Answer A. &
Explanation The single ampersand (&) is the Bitwise AND operator. && is not a valid Python operator. and is the Logical AND operator.

Q 12. What is the output of the Python expression 5 & 3?

A. 7

B. 1

C. 5

D. 3

Show Answer B. 1
Explanation $5$ is 0101, $3$ is 0011. 0101 & 0011 = 0001, which is $1$.

Q 13. Which of the following is the Python Bitwise OR operator?

A. |

B. ||

C. or

D. ^

Show Answer A. |
Explanation `**

Q 14. What is the output of the Python expression 5 | 3?

A. 7

B. 1

C. 5

D. 3

Show Answer A. 7
Explanation $5$ is 0101, $3$ is 0011$. 0101

Q 15. Which of the following is the Python Bitwise XOR operator?

A. ^

B. xor

C. XOR

D. ~

Show Answer A. ^
Explanation The caret (^) is the Bitwise XOR (Exclusive OR) operator.

Q 16. What is the output of the Python expression 5 ^ 3?

A. 7

B. 6

C. 4

D. 2

Show Answer B. 6
Explanation $5$ is 0101, $3$ is 0011$. 0101 ^ 0011 = 0110$, which is $6$.

Q 17. Which operator is used for the Bitwise NOT (one’s complement) operation in Python?

A. !

B. ~

C. –

D. not

Show Answer B. ~
Explanation The tilde (~) is the Bitwise NOT operator, which calculates the one’s complement.

Q 18. What is the output of the Python expression ~5?

A. 5

B. -5

C. 6

D. -6

Show Answer D. -6
Explanation In Python, the bitwise NOT of $x$ is equivalent to $-(x+1)$. So, $\sim 5 = -(5+1) = -6$.

Q 19. What does the expression a << n represent in Python?

A. Bitwise right shift of $a$ by $n$ bits.

B. Arithmetic shift of $a$ by $n$ bits.

C. Bitwise left shift of $a$ by $n$ bits.

D. Logical AND of $a$ and $n$.

Show Answer C. Bitwise left shift of $a$ by $n$ bits.
Explanation << is the Bitwise Left Shift operator.

Q 20. What is the output of the Python expression 4 << 2?

A. 1

B. 8

C. 16

D. 4

Show Answer C. 16
Explanation $4 \ll 2$ is equivalent to $4 \times 2^2 = 4 \times 4 = 16$.

Q 21. What does the expression a >> n represent in Python?

A. Bitwise left shift of $a$ by $n$ bits.

B. Bitwise right shift of $a$ by $n$ bits.

C. Arithmetic shift of $a$ by $n$ bits.

D. Logical OR of $a$ and $n$.

Show Answer B. Bitwise right shift of $a$ by $n$ bits.
Explanation >> is the Bitwise Right Shift operator.

Q 22. What is the output of the Python expression 16 >> 3?

A. 0

B. 1

C. 2

D. 8

Show Answer C. 2
Explanation $16 \gg 3$ is equivalent to integer division $16 / 2^3 = 16 / 8 = 2$.

Q 23. In Python, how is the bitwise right shift (>>) implemented for negative numbers?

A. Logical right shift (fills with 0).

B. Arithmetic right shift (fills with the sign bit).

C. Raises a TypeError.

D. Converts to positive, shifts, and then negates.

Show Answer B. Arithmetic right shift (fills with the sign bit).
Explanation Python uses Arithmetic Right Shift for signed integers, meaning the sign bit (leftmost bit) is preserved (filled from the left) to maintain the sign.

Q 24. Which bitwise operation can be used to efficiently check if a number is even or odd?

A. num | 1

B. num & 1

C. num ^ 1

D. num >> 1

Show Answer B. num & 1
Explanation An integer is odd if its least significant bit (LSB) is 1, and even if it’s 0. num & 1 will be $\mathbf{1}$ for odd numbers and $\mathbf{0}$ for even numbers.

Q 25. If a = 10 and b = 5, what is the result of a & b?

A. 0

B. 2

C. 8

D. 1

Show Answer A. 0
Explanation $10$ is 1010, $5$ is 0101$. 1010 & 0101 = 0000$, which is $0$.

Q 26. Which bitwise operation is commonly used to “set” (turn on) a specific bit at a given position?

A. Bitwise AND (&)

B. Bitwise OR (|)

C. Bitwise XOR (^)

D. Bitwise NOT (~)

Show Answer B. Bitwise OR (|)
Explanation `)**

Q 27. The expression n << 1 is equivalent to which arithmetic operation?

A. $n / 2$

B. $n \times 2$

C. $n + 1$

D. $n – 1$

Show Answer B. $n \times 2$
Explanation A left shift by one position is equivalent to multiplying the number by 2.

Q 28. What will be the value of x after the code: x = 7; x &= 3;?

A. 3

B. 7

C. 1

D. 0

Show Answer C. 1
Explanation $7$ is 0111, $3$ is 0011$. x &= 3` is $7 \text{ } \& \text{ } 3$. $0111 \text{ } \& \text{ } 0011 = 0001$, which is $1$.

Q 29. Which bitwise operator is used to swap two numbers without using a temporary variable?

A. &

B. |

C. ^

D. ~

Show Answer C. ^
Explanation The XOR swap algorithm uses three XOR operations: $a = a \text{ } ^ \text{ } b; b = a \text{ } ^ \text{ } b; a = a \text{ } ^ \text{ } b$.

Q 30. In Python, bitwise operators only work on which data type?

A. Floats

B. Integers

C. Strings

D. Booleans

Show Answer B. Integers
Explanation Bitwise operators are specifically designed to work on the binary representation of integers.

Q 31. What is the decimal value of the binary number 0b1010?

A. 8

B. 10

C. 12

D. 16

Show Answer B. 10
Explanation $0b1010 = 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 0 \times 2^0 = 8 + 0 + 2 + 0 = 10$.

Q 32. How can you “clear” (turn off) a specific bit at position $k$ in a number $n$ using a bitmask?

A. $n \text{ } | \text{ } (1 << k)$

B. $n \text{ } \& \text{ } (1 << k)$

C. $n \text{ } \& \text{ } \sim (1 << k)$

D. $n \text{ } ^ \text{ } (1 << k)$

Show Answer C. $n \text{ } \& \text{ } \sim (1 << k)$
Explanation $\sim (1 << k)$ creates a mask with a 0 only at position $k$ and 1s everywhere else. ANDing $n$ with this mask clears the $k$-th bit without affecting others.

Q 33. What is the output of the expression ~-1?

A. 0

B. -1

C. 1

D. 2

Show Answer A. 0
Explanation Using the formula $\sim x = -(x+1)$, we get $\sim (-1) = -(-1+1) = -(0) = 0$.

Q 34. What will be the output of bin(10)?

A. 1010

B. 0b10

C. 0b1010

D. 0b1001

Show Answer C. 0b1010
Explanation The built-in bin() function returns the binary string representation of an integer, prefixed with 0b. $10$ is binary $1010$.

Q 35. Which bitwise operation is the same as dividing an unsigned integer by $2^n$ and discarding the fractional part?

A. Left Shift (<<)

B. Right Shift (>>)

C. Bitwise AND (&)

D. Bitwise NOT (~)

Show Answer B. Right Shift (>>)
Explanation A right shift by $n$ positions (>> n) is equivalent to floor division by $2^n$ ($x // 2^n$) for positive numbers.

Q 36. If x = 10 (binary 0b1010), what is the value of x | 1?

A. 10

B. 11

C. 9

D. 8

Show Answer B. 11
Explanation $10$ is 1010, $1$ is 0001$. 1010

Q 37. What is the bitwise representation of -2 in Python (using a typical Two’s Complement interpretation for an example)?

A. ~1

B. ~-3

C. ~2

D. ~-1

Show Answer A. ~1
Explanation $\sim 1 = -(1+1) = -2$.

Q 38. If $k$ is an integer, for what value of $k$ will $k \text{ } \& \text{ } (k – 1)$ result in 0?

A. Any even number

B. Any odd number

C. Any power of 2

D. Any negative number

Show Answer C. Any power of 2
Explanation If $k$ is a power of 2 (e.g., 4: 0100), then $k-1$ will be all 1s up to that bit (e.g., 3: 0011). The result of the AND operation will be $\mathbf{0}$ because they share no set bits.

Q 39. What is the output of 0xF & 0xA (where 0x denotes a hexadecimal number)?

A. 10

B. 15

C. 5

D. 0

Show Answer A. 10
Explanation $0xF$ is $15$ (1111), $0xA$ is $10$ (1010). $1111 \text{ } \& \text{ } 1010 = 1010$, which is $10$ in decimal.

Q 40. Which compound assignment operator is not one of the Python bitwise assignment operators?

A. &=

B. |==

C. ^=

D. <<=

Show Answer B. |==
Explanation ==`**

Comments

Leave a Reply

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