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

Bitwise Operators Questions and Answers

11.

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.

12.

Bitwise & can be used to check if a bit in number is set or not.

Answer: A

Correct answer: True

This is the standard bit test. Build a mask with a single 1 in the position of interest, usually as 1 << k, and AND it with the value: n & (1 << k) is non-zero when the bit is set and zero when it is clear.

Note that the non-zero result is \( 2^k \), not 1, so the expression should be tested against zero rather than compared with 1. AND is the natural operator for the job because it forces every unselected position to zero, leaving only the bit being examined.

Please log in or register to report this question.

13.

What will be the output of the program?

#define P printf("%d\n", -1^~0);
#define M(P) int main()\
             {\
                P\
                return 0;\
             }
M(P)

 

Answer: D

Correct answer: 0

The macro machinery is a distraction: M(P) expands to a main containing the printf, so the whole program reduces to printing -1 ^ ~0.

Now the arithmetic. ~0 inverts every bit of zero, giving all ones, which as a signed int is \( -1 \). So the expression is -1 ^ -1, and XOR of any value with itself is zero, since no bit position differs from itself. The answer is 0 regardless of the width of an int, which is what makes the identity \( x \mathbin{\hat{}} x = 0 \) worth knowing.

Please log in or register to report this question.

14.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        int c = 2 ^ 3;
        printf("%d\n", c);
    }

 

Answer: A

Correct answer: 1

^ is the bitwise exclusive OR, not exponentiation - C has no exponentiation operator. Work in binary:

\[ 2 = 10_2 \qquad 3 = 11_2 \]

XOR sets each result bit where the two input bits differ. The high bits are both 1, so that bit is 0; the low bits are 0 and 1, so that bit is 1. The result is \( 01_2 = 1 \). The distractor 9 is what \( 2^3 \) would give if ^ meant a power, and 7 is what | would give.

Please log in or register to report this question.

15.

Consider the given statement:

int x = 10 ^ 2

What will be the value of x?

Answer: D

Correct answer: 8

^ is bitwise XOR, not exponentiation - C has no exponentiation operator, and pow() from math.h is what would compute \( 10^2 \).

In binary:

\[ 10 = 1010_2 \qquad 2 = 0010_2 \]

XOR sets a result bit where the inputs differ. They differ only in the fourth position, giving \( 1000_2 = 8 \). Reading ^ as a power would give 100, which is not even among the options - the distractors instead cluster around the correct answer to catch arithmetic slips.

Please log in or register to report this question.

16.

Predict the output of following program.

#include <stdio.h>

int main()
{
 char flag=0x0f;

 flag &= ~0x02;
 printf("%d",flag);

 return 0;
}

 

Answer: A

Correct answer: 13

Work through the mask. 0x0f is \( 1111_2 \) and 0x02 is \( 0010_2 \), so ~0x02 is ...11111101 - every bit set except bit 1.

AND then clears exactly that bit and leaves the others: \( 1111_2 \mathbin{\&} 1101_2 = 1101_2 = 13 \). This is the clear-a-bit idiom, flag &= ~mask. The distractor d is the same value in hexadecimal, which %d does not print - it prints the decimal 13.

Please log in or register to report this question.

17.

Left shifting an unsigned int or char by 1 is always equivalent to multiplying it by 2.

Answer: A

Correct answer: True

For unsigned types the arithmetic is defined to wrap modulo \( 2^n \), so a left shift by one is exactly a multiplication by two with any overflow discarded rather than undefined.

That is the whole difference from the signed case, where shifting a value whose significant bits reach the sign position is undefined behaviour. The char in the question is promoted to int before the shift, so the operation happens in a wider type; the guarantee is what makes shift-based multiplication safe on unsigned values and unsafe on signed ones.

Please log in or register to report this question.

18.

Left shift (<<) and Right shift (>>) operators are equivalent to _____________ by 2.

Choose the correct words...

Answer: A

Correct answer: Multiplication and Division

Each position in a binary number carries twice the weight of the one to its right, so moving every bit one place left doubles the value and moving it one place right halves it:

\[ n \ll k = n \times 2^{k} \qquad n \gg k = n \div 2^{k} \]

Two cautions attach. The right shift truncates towards zero for unsigned values, discarding the bits shifted out, so it is integer division. And the left shift is a reliable multiplication only for unsigned types, since a signed overflow is undefined behaviour.

Please log in or register to report this question.

19.

Which is not a bitwise operator?

Answer: D

Correct answer: &&

&& is the logical AND. It treats each operand as a whole truth value, yields only 0 or 1, and short-circuits - the right operand is not evaluated if the left already settles the result.

The other three are bitwise: & and | combine operands position by position, and << moves bits left. The single-character and doubled forms are the classic confusion: 4 & 8 is 0 because the numbers share no bits, while 4 && 8 is 1 because both are non-zero.

Please log in or register to report this question.

20.

Bitwise | can be used to multiply a number by powers of 2.

Answer: B

Correct answer: No

Multiplication by a power of two is the left shift's job: n << k multiplies by \( 2^k \).

OR cannot do it, because it only ever sets bits and never moves them. It happens to look like addition when the operands have no bits in common - \( 4 \mathbin{|} 8 = 12 \) is also \( 4 + 8 \) - but that coincidence fails the moment any position overlaps, since there is no carry: \( 3 \mathbin{|} 1 = 3 \) where \( 3 + 1 = 4 \).

Please log in or register to report this question.

Loading…
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