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

Bitwise Operators Questions and Answers

51.

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.

52.

What will be the output of the program?

#include <stdio.h>

int main()
{
    unsigned char i = 0x80;
    printf("%d\n", i<<1);
    return 0;
}

 

Answer: A

Correct answer: 256

The key step is integer promotion. i is an unsigned char holding 0x80, which is 128, but before the shift it is promoted to int - the usual arithmetic conversions apply to the operand of a shift.

The shift therefore happens in a type wide enough to hold the result:

\[ 128 \ll 1 = 256 \]

Had the arithmetic been done in the original 8-bit width, the 1 would have shifted out of the top and the answer would have been 0. Promotion is why it is not, and why %d prints an int rather than a char.

Please log in or register to report this question.

53.

Bitwise can be used to reverse a sign of a number.

Answer: B

Correct answer: No

Negation in two's complement is ~x + 1, and that + 1 is ordinary addition, not a bitwise operation. Inverting the bits alone gives \( -x - 1 \), which is one short of the answer.

The reason is that bitwise operators work on each position independently and have no mechanism to propagate a carry, and changing a number's sign requires exactly that carry. XOR with a sign-bit mask flips the sign bit of a floating-point value, but that is a property of the IEEE format rather than of integer arithmetic.

Please log in or register to report this question.

54.

Bitwise can be used to perform addition and subtraction.

Answer: B

Correct answer: No

Addition requires carries to propagate from one bit position to the next, and bitwise operators do not carry - each output bit depends only on the input bits in that same position.

XOR gives the sum without carries and AND gives where the carries arise, but combining them into an addition takes a loop that shifts the carries left and repeats until none remain. That loop is ordinary control flow, not a bitwise operator, so no single bitwise expression performs addition or subtraction.

Please log in or register to report this question.

55.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        int y = 0;
        if (1 |(y = 1))
            printf("y is %d\n", y);
        else
            printf("%d\n", y);
 
    }

 

Answer: A

Correct answer: y is 1

| is the bitwise OR, not the logical ||, and that difference decides the answer. Bitwise operators do not short-circuit, so both operands are evaluated whatever the left one contains.

The assignment (y = 1) therefore runs, setting y to 1, and the OR gives 1 | 1 = 1, which is true. The printf shows y is 1. Had the code used ||, the left operand 1 would have been enough to decide the result, the assignment would have been skipped, and y would still be 0.

Please log in or register to report this question.

56.

What will be the output of the program?

#include <stdio.h>

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

Answer: C

Correct answer: 8

The expression x & (x - 1) is a well-known idiom: it clears the lowest set bit of x.

Here \( x = 10 \), which is 1010, so \( x - 1 = 9 \), which is 1001. Subtracting one flips the lowest set bit to 0 and turns every zero below it into a 1 - that is what borrowing does in binary. ANDing the two therefore agrees on everything above that bit and disagrees at it and below:

  10 = 1 0 1 0
   9 = 1 0 0 1
  -----------
   & = 1 0 0 0

The result 1000 is 8.

The idiom has two common uses. Repeating it until the value reaches zero counts the set bits in as many steps as there are ones - Kernighan’s algorithm. And a single application testing x & (x - 1) == 0 identifies a power of two, since only those have exactly one bit set.

Please log in or register to report this question.

57.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        unsigned int a = 10;
        a = ~a;
        printf("%d\n", a);
    }

 

Answer: C

Correct answer: -11

Two things happen here. ~ inverts every bit of a, and on a 32-bit machine 10 is 0x0000000A, so ~a is 0xFFFFFFF5.

Then %d prints that bit pattern as a signed int, even though a was declared unsigned. Interpreted as two's complement, 0xFFFFFFF5 is \( -11 \). The general identity is worth remembering:

\[ \mathtt{\sim}x = -x - 1 \]

so \( \mathtt{\sim}10 = -11 \) directly. Printing an unsigned value with %d is itself a mismatch, which is what exposes the bit pattern.

Please log in or register to report this question.

58.

What will be the output of the program?

#include <stdio.h>

int main()
{
    char c=48;
    int i, mask=01;
    for(i=1; i<=5; i++)
    {
        printf("%c", c|mask);
        mask = mask<<1;
    }
    return 0;
}

 

Answer: B

Correct answer: 12480

Trace the loop. c is 48, which is \( 110000_2 \), the character '0'. Each pass ORs it with the mask and prints the result as a character, then shifts the mask left.

Masks 1, 2, 4 and 8 set bits that 48 does not have, giving 49, 50, 52 and 56 - the characters 1, 2, 4 and 8.

The fifth mask is 16, and 48 is \( 32 + 16 \), so bit 4 is already set: OR changes nothing and 48 prints as 0. The output is 12480, and the final zero is the whole point - OR is idempotent.

Please log in or register to report this question.

59.

For an unsigned integer, right shifting by 1 is equivalent to integer division by 2.

Answer: A

Correct answer: True

Each right shift discards the least significant bit and halves the remaining value, with the fractional part truncated. The equivalence holds only for unsigned types, since the sign bit makes the signed case implementation-defined.

Please log in or register to report this question.

60.

Predict the output of following program.

#include <stdio.h>

int main()
{
 int a=10;
 int b=2;
 int c;
 
 c=(a & b);
 printf("c= %d",c);
 
 return 0;
}

 

Answer: C

Correct answer: c= 2

Write both operands in binary and AND them position by position:

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

A result bit is 1 only where both inputs are 1, which happens in the second position alone, giving \( 0010_2 = 2 \).

The distractor 12 is what | would give and 0 is what ^ would give in the other direction - useful checks, since 10 and 2 share exactly one bit. Note that && would have given 1, both operands being non-zero.

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