Machine-translated by Google. Questions are authored in English.
Top

Bitwise Operators Questions and Answers

71.

The expression n & 1 evaluates to 1 exactly when n is odd.

Answer: A

Correct answer: True

The least significant bit carries the value 1, so it is set precisely for odd numbers. Masking with 1 is therefore a faster parity test than taking the remainder, and unlike the remainder operator it behaves the same for negative values in two’s complement.

Please log in or register to report this question.

72.

Shifting an integer by a number of bits greater than or equal to its width is well defined in C.

Answer: B

Correct answer: False

Such a shift is undefined behaviour: the standard requires the shift count to be non-negative and less than the width of the promoted left operand. Different processors handle an oversized count differently, which is why no result is guaranteed.

Please log in or register to report this question.

73.

A non-zero integer n is a power of two exactly when n & (n - 1) equals zero.

Answer: A

Correct answer: True

A power of two has exactly one bit set, so clearing its lowest set bit leaves nothing. Any value with two or more bits set retains the higher ones, giving a non-zero result, which is why the test must also exclude n = 0.

Please log in or register to report this question.

74.

On left shifting, the bits from the left are rotated and brought to the right and accommodated where there is empty space on the right?

Answer: B

Correct answer: False

C's shift operators do not rotate. On a left shift the bits leaving the top are simply discarded, and zeros are brought in at the bottom - nothing wraps around.

Rotation exists in most processors as a machine instruction but has no operator in C, and must be written out: (x << k) | (x >> (n-k)) on an unsigned type of \( n \) bits, which reintroduces at the bottom the bits the left shift discarded. Discarding rather than rotating is what makes a left shift a multiplication by two.

Please log in or register to report this question.

75.

The bitwise XOR of any integer with itself is always zero.

Answer: A

Correct answer: True

XOR yields 1 only where the two operands differ, and a value never differs from itself, so every bit of the result is 0. This identity is what lets XOR be used to detect a change and to cancel a value out of a running total.

Please log in or register to report this question.

76.

The expression n & (n - 1) clears the lowest set bit of n.

Answer: B

Correct answer: True

Subtracting 1 turns the lowest set bit to 0 and every bit below it to 1, so ANDing with the original leaves that bit and all below it clear. Repeating the operation until n becomes 0 counts the set bits, which is Kernighan’s algorithm.

Please log in or register to report this question.

77.

In C, the bitwise AND operator has higher precedence than the equality operator ==.

Answer: B

Correct answer: False

Equality binds more tightly, so the expression a & b == c is parsed as a & (b == c), which is almost never what the programmer intended. Writing the intended grouping with explicit parentheses is the standard defence.

Please log in or register to report this question.

78.

In C, bitwise operators can be applied to floating-point operands.

Answer: B

Correct answer: False

The bitwise operators require operands of integer type; applying one to a float or double is a compile-time error. To manipulate the bits of a floating-point value the program must first reinterpret it through an integer type of the same width.

Please log in or register to report this question.

79.

In C, right shifting a negative signed integer gives an implementation-defined result.

Answer: A

Correct answer: True

The standard leaves it to the implementation whether the vacated high bits are filled with zeros or with copies of the sign bit. Most compilers perform an arithmetic shift that preserves the sign, but portable code should not rely on it.

Please log in or register to report this question.

80.

Taking the bitwise OR of a number with 0 leaves the number unchanged.

Answer: B

Correct answer: True

ORing with 0 leaves each bit as it was, so 0 is the identity element for OR. The corresponding identity for AND is a value with all bits set, and for XOR it is again 0.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Bitwise Operators Questions and Answers || Bitwise Operators MCQ Questions and Answers || Bitwise Operators GK Questions and Answers || Bitwise Operators GK MCQ Questions || Bitwise Operators Multiple Choice Questions and Answers || Bitwise Operators GK || GK on Bitwise Operators || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming