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

Bitwise Operators Questions and Answers

31.

Predict the output of following program.

#include <stdio.h>

int main()
{
    int x=10;
    
    x &= ~2;
    printf("x= %d",x);
    
    return 0;
}

 

Answer: B

Correct answer: x= 8

This is the clear-a-bit idiom, x &= ~mask. Build the mask first: 2 is \( 0010_2 \), so ~2 is ...11111101 - every bit set except bit 1.

ANDing forces that one position to 0 and leaves the rest untouched:

\[ 1010_2 \mathbin{\&} 1101_2 = 1000_2 = 8 \]

So the operation removes the value 2 from x. The distractor 12 is what x |= 2 would give, and 10 is what would result if the bit had not been set in the first place.

Please log in or register to report this question.

32.

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.

33.

What will be the output of the program?

#include <stdio.h>

int main()
{
    printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
    return 0;
}

 

Answer: C

Correct answer: 2 >> 4 Garbage value >> Garbage value

The format string contains four %d conversions but only two arguments are supplied. The first two consume \( 4 \gg 1 = 2 \) and \( 8 \gg 1 = 4 \); the remaining two read whatever happens to lie where further arguments would have been.

The literal >> characters in the format are printed as text, which is what makes the output look like shift expressions. The behaviour is undefined, so the garbage is not merely unpredictable but unconstrained - and no diagnostic is required, which is why mismatched printf arguments are such a persistent source of bugs.

Please log in or register to report this question.

34.

What is the value of the expression 13 & 25?

Answer: D

Correct answer: 9

AND compares the two numbers bit by bit and keeps a 1 only where both have one. Line them up in binary:

  13 = 0 1 1 0 1
  25 = 1 1 0 0 1
  --------------
   &  = 0 1 0 0 1

Column by column: 0 and 1 give 0; 1 and 1 give 1; 1 and 0 give 0; 0 and 0 give 0; 1 and 1 give 1. The result 01001 is \( 8 + 1 = 9 \).

Take care not to confuse & with the logical &&: the logical operator would treat both numbers as merely true and yield 1. The distractor 29 is what | gives on the same pair, and 20 is what ^ gives - useful to know, because \( 9 + 20 = 29 \) is no coincidence: AND and XOR partition the bits that OR collects.

Please log in or register to report this question.

35.

Which expression clears the third bit from the right of an int variable n, counting the least significant bit as bit 0?

Answer: B

Correct answer: n &= ~(1 << 2)

Clearing a bit means forcing it to 0 while leaving every other bit alone, and the idiom for that is AND with the complement of a mask.

Build the mask first. Bits are numbered from 0 at the right, so the third bit from the right is bit 2, and 1 << 2 gives 00000100. Complementing it with ~ gives 11111011 - ones everywhere except the position of interest. ANDing with that leaves every other bit unchanged, since \( b \wedge 1 = b \), and forces bit 2 to zero, since \( b \wedge 0 = 0 \). So n &= ~(1 << 2) is the answer.

The distractors each get one part wrong. ~(1 << 3) builds the mask for bit 3, the fourth bit from the right - the classic off-by-one that comes of counting from 1 instead of 0. |= with 1 << 2 does the opposite job, setting the bit rather than clearing it. And ^= toggles the bit, so it clears it only if it happened to be set already.

Please log in or register to report this question.

36.

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.

37.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int i=4, j=8;
    printf("%d, %d, %d\n", i|j&j|i, i|j&&j|i, i^j);
    return 0;
}

 

Answer: C

Correct answer: 12, 1, 12

Precedence decides the first two, and the operators differ in kind.

In i|j&j|i, & binds tighter than |, so it is i | (j&j) | i = 4 | 8 | 4 = 12, since 4 and 8 occupy different bits.

In i|j&&j|i, the logical && binds looser than |, so it is (i|j) && (j|i) = 12 && 12, and a logical operator yields 1.

Finally \( 4 \mathbin{\hat{}} 8 = 12 \), the bits again being disjoint. Bitwise operators sit above the logical ones in the precedence table, which is the point.

Please log in or register to report this question.

38.

Which of the following statements are correct about the program?

#include <stdio.h>

int main()
{
    unsigned int m[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
    unsigned char n, i;
    scanf("%d", &n);
    for(i=0; i<=7; i++)
    {
        if(n & m[i])
            printf("yes");
    }
    return 0;
}

 

Answer: B

Correct answer: It will test whether the individual bits of n are ON or OFF

The array m holds masks with exactly one bit set each, from 0x01 through 0x80 - the eight bit positions of a byte. The loop ANDs n with each in turn and prints yes whenever the result is non-zero.

AND with a single-bit mask isolates that bit and discards the rest, so the program reports which bits of n are set. Nothing is assigned back to n, so no bits are changed - the first and third options describe modifications the code never makes.

Please log in or register to report this question.

39.

Bitwise & can be used to divide a number by powers of 2

Answer: B

Correct answer: False

Division by a power of two is the right shift's job: n >> k divides by \( 2^k \), discarding the bits shifted out.

What AND does with a power-of-two mask is the complementary operation - it keeps the low bits and so gives the remainder: n & (8-1) is n % 8 for unsigned n. So AND computes the modulus, not the quotient. The two together, shift for the quotient and mask for the remainder, are how a compiler replaces division by a constant power of two.

Please log in or register to report this question.

40.

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

Answer: D

Correct answer: | operator

Turning a bit on means forcing it to 1 regardless of its present state, and OR does exactly that: \( b \mathbin{|} 1 = 1 \) for any \( b \), while \( b \mathbin{|} 0 = b \) leaves the rest alone.

The idiom is n |= (1 << k). Note the symmetry with clearing a bit: OR with a mask of ones to set, AND with a mask of zeros to clear, and XOR with ones to toggle. || is the logical OR and would collapse the whole value to 0 or 1, which is not what is wanted.

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