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

Bitwise Operators Questions and Answers

41.

Left shifting a number by 1 is always equivalent to multiplying it by 2.

Answer: A

Correct answer: False

The equivalence holds only while the result fits. Left-shifting a signed value whose top bits are significant overflows, and signed overflow is undefined behaviour in C - on a 32-bit int, 0x40000000 << 1 does not give twice that value but a negative number.

Shifting a negative value left is undefined outright. The statement is true when restricted to unsigned types, where the arithmetic is defined to wrap modulo \( 2^n \), which is precisely why the companion question about unsigned int and char answers the other way.

Please log in or register to report this question.

42.

Bitwise & can be used to check if more than one bit in a number is on.

Answer: A

Correct answer: True

AND with a mask isolates exactly the bits the mask selects, and a mask may name as many positions as wanted. To test whether any of several bits is set, OR the masks together and AND once: if (n & (BIT2 | BIT5)) is true if either is on.

Testing that all of them are on needs a comparison rather than a mere test: if ((n & mask) == mask). That distinction - non-zero means at least one, equality means all - is the practical point behind the question.

Please log in or register to report this question.

43.

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.

44.

In which numbering system can the binary number 1011011111000101 be easily converted to?

Answer: B

Correct answer: Hexadecimal system

Sixteen is a power of two, so each hexadecimal digit corresponds to exactly four binary digits and the conversion is a matter of grouping rather than of arithmetic.

Splitting the number from the right gives 1011 0111 1100 0101, which reads directly as B7C5. Octal shares the property with groups of three bits, but sixteen bits divide evenly into four hexadecimal digits and not into whole octal ones, which is why hex is the convention for byte-oriented data. Decimal is a power of ten and requires repeated division.

Please log in or register to report this question.

45.

Which bitwise operator is suitable for turning off a particular bit in a number?

Answer: B

Correct answer: & operator

Turning a bit off means forcing it to 0 while leaving the others untouched, and only AND can do that: \( b \mathbin{\&} 0 = 0 \) for any \( b \), while \( b \mathbin{\&} 1 = b \).

The idiom is therefore n &= ~(1 << k) - build a mask with a single 1 in position \( k \), invert it so that position alone is 0, and AND. && and || are logical operators yielding only 0 or 1 and destroying the other bits entirely; ! is logical negation of the whole value.

Please log in or register to report this question.

46.

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.

47.

What will be the output of the program?

#include <stdio.h>

int main()
{
    int i = 4;
    i = i << 2 | i >> 1;
    printf("%d", i);
    return 0;
}

Answer: C

Correct answer: 18

The key is operator precedence: the shift operators bind more tightly than the bitwise OR, so the expression groups as (i << 2) | (i >> 1), not as some left-to-right reading.

With \( i = 4 \), which is 00000100:

i << 2 moves the bits two places left, giving 00010000 = 16 - a left shift by \( n \) multiplies by \( 2^{n} \), and \( 4 \times 4 = 16 \).
i >> 1 moves them one place right, giving 00000010 = 2, that is \( 4 / 2 \).

ORing the two combines their set bits, and since they occupy different positions nothing overlaps: 00010000 | 00000010 = 00010010 = \( 16 + 2 = \) 18.

Both shifts read the original value of i; the assignment happens only once the whole right-hand side has been evaluated, so the left shift does not feed the right one.

Please log in or register to report this question.

48.

In the statement expression1 >> expression2. if expression1 is a signed integer with its leftmost bit set to 1 then on right shifting it the result of the statement will vary from computer to computer

Answer: A

Correct answer: True

Right-shifting a signed value with the sign bit set is implementation-defined in C: the standard permits either an arithmetic shift, propagating the sign bit so the value stays negative, or a logical shift, bringing in zeros so it becomes a large positive number.

In practice every common compiler chooses the arithmetic shift, but portable code cannot rely on it because the standard does not require it. The safe course is to cast to an unsigned type when the bit pattern is wanted, and to divide when the arithmetic result is wanted.

Please log in or register to report this question.

49.

Two integer variables can be swapped using the XOR operator without any temporary variable.

Answer: A

Correct answer: True

Applying a = a ^ b, then b = a ^ b, then a = a ^ b leaves the two values exchanged, because XOR is its own inverse. The trick fails when both names refer to the same object, since the variable then XORs with itself and becomes zero.

Please log in or register to report this question.

50.

If an unsigned int is 2 bytes wide then, What will be the output of the program ?

#include <stdio.h>

int main()
{
    unsigned int a=0xffff;
    ~a;
    printf("%x\n", a);
    return 0;
}

 

Answer: A

Correct answer: ffff

The statement ~a; computes a value and throws it away. ~ has no side effect on its operand - only an assignment such as a = ~a or the compound form would change a.

So a is still 0xffff when it is printed. Most compilers warn about a statement with no effect, which is the practical defence against this mistake. The intended complement would have been 0x0000, which is the distractor the question offers.

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