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

Bitwise Operators Questions and Answers

71.

In C, the value of ~0u is the largest value an unsigned int can hold.

Answer: A

Correct answer: True

The literal 0u is an unsigned int of value zero, and complementing it sets every bit, which is by definition UINT_MAX. This is a common portable idiom for obtaining an all-ones mask without naming the type’s width.

Please log in or register to report this question.

72.

Bitwise & and | are unary operators

Answer: B

Correct answer: False

Both are binary operators: each takes two operands and combines them bit by bit, as in a & b and a | b.

C's only unary bitwise operator is ~, the one's complement, which inverts every bit of a single operand. The confusion usually comes from & also serving as the unary address-of operator in a quite different context - &x against a & b - but that is a distinct operator sharing the same symbol, not the bitwise AND used with one operand.

Please log in or register to report this question.

73.

What will be the output of the program?

#include <stdio.h>

int main()
{
    printf("%d %d\n", 32<<1, 32<<0);
    printf("%d %d\n", 32<<-1, 32<<-0);
    printf("%d %d\n", 32>>1, 32>>0);
    printf("%d %d\n", 32>>-1, 32>>-0);
    return 0;
}

Answer: B

Correct answer: 64 32, 0 32, 16 32, 0 32

Take the four lines in turn, remembering that << shifts the bits left, multiplying by a power of two, and >> shifts right, dividing by one.

32<<1 doubles 32 to 64, and 32<<0 shifts by nothing, leaving 32. First line: 64 32.

The third line is the mirror image: 32>>1 halves 32 to 16, and 32>>0 leaves 32. Third line: 16 32.

The second and fourth lines are the interesting ones. -0 is simply 0 in two’s complement - there is no negative zero for integers - so 32<<-0 and 32>>-0 are shifts by zero and give 32. A shift by -1, however, is undefined behaviour: the standard says the result is undefined when the right operand is negative or not less than the width of the promoted left operand. In practice the x86 shift instructions mask the count to its low five bits, so -1 becomes 31; 32<<31 overflows a 32-bit int to 0, and 32>>31 shifts every set bit off the right-hand end, also giving 0. Second and fourth lines: 0 32.

The output is therefore 64 32, 0 32, 16 32, 0 32. It is worth being clear that only the first and third lines are guaranteed by the language; the zeros on the other two are what a typical compiler on this hardware produces, not what the standard promises, and a compiler is free to emit anything at all for them. This record had lost two of the four printf lines to a stray < in the stored HTML; they are restored above, which is what makes the keyed sequence of eight numbers legible.

Please log in or register to report this question.

74.

What will be the output of the following C code?

#include <stdio.h>
    void main()
    {
        int x = 97;
        int y = sizeof(x++);
        printf("x is %d", x);
    }

 

Answer: A

Correct answer: x is 97

sizeof does not evaluate its operand. The compiler needs only the type of the expression to determine its size, and that is known at compile time, so x++ is never executed.

Consequently y receives the size of an int and x remains 97. This is a genuine and easily missed rule: any side effect written inside sizeof - an increment, an assignment, even a function call - simply does not happen. The one exception is a variable-length array type, whose size must be computed at run time.

Please log in or register to report this question.

75.

What will be the output of the program?

#include <stdio.h>

int main()
{
    unsigned int res;
    res = (64 >>(2+1-2)) & (~(1<<2));
    printf("%d\n", res);
    return 0;
}

 

Answer: A

Correct answer: 32

Take the two halves separately. The shift count evaluates first: \( 2+1-2 = 1 \), so the left operand is \( 64 \gg 1 = 32 \).

The right operand is ~(1<<2), a mask with every bit set except bit 2 - the clear-bit-2 mask.

Now \( 32 = 100000_2 \) has its only bit in position 5, which the mask leaves alone, so the AND changes nothing:

\[ 32 \mathbin{\&} \mathtt{\sim}4 = 32 \]

The masking is a decoy: it would have mattered only if the value had bit 2 set.

Please log in or register to report this question.

76.

Which of the following statements are correct about the program?

#include <stdio.h>

int main()
{
    unsigned int num;
    int c=0;
    scanf("%u", &num);
    for(;num;num>>=1)
    {
        if(num & 1)
            c++;
    }
    printf("%d", c);
    return 0;
}

 

Answer: A

Correct answer: It counts the number of bits that are ON (1) in the number num.

The loop is the standard population count. Each pass tests the lowest bit with num & 1, incrementing c when it is set, then shifts num right by one so the next bit takes that position.

The loop condition num stops as soon as the value reaches zero, so it runs once per significant bit rather than a fixed number of times. Declaring num unsigned matters: right-shifting a negative signed value propagates the sign bit and the loop would never terminate.

Please log in or register to report this question.

77.

Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?

Answer: A

Correct answer: Bitwise AND (&)

Parity lives entirely in the lowest bit: a binary number is odd exactly when bit 0 is 1, because every higher place value is a multiple of two.

So n & 1 isolates that bit and answers the question in one operation - non-zero for odd, zero for even. It is faster than n % 2 and, unlike the modulus, gives the right answer for negative numbers in two's complement, where % returns \( -1 \) for an odd negative value. OR and XOR combine bits rather than isolating one, and ~ inverts the whole value.

Please log in or register to report this question.

78.

What will be the output of the program?

#include <stdio.h>

int main()
{
    int n = 12;
    printf("%d %d", n & 1, n >> 1);
    return 0;
}

Answer: C

Correct answer: 0 6

Two independent expressions are printed.

n & 1 isolates the least significant bit, which is the parity of the number: it is 1 for odd values and 0 for even ones. Since \( 12 \) is 1100, the low bit is 0, so the first value printed is 0. This is the usual fast alternative to n % 2, and it behaves better on negative numbers, where % in C yields \(-1\) rather than 1.

n >> 1 shifts every bit one place right, discarding the low bit: 1100 becomes 0110, which is 6. That is integer division by two, \( 12 / 2 = 6 \).

So the output is 0 6. Note that the two operations are complementary - the shift keeps everything the mask throws away - which is why they appear together when a number is consumed bit by bit.

Please log in or register to report this question.

79.

Predict the output of following program.

#include <stdio.h>

int main()
{
 char var=0x04;

 var = var | 0x04;
 printf("%d,",var);
 var |= 0x01;
 printf("%d",var);
 
 return 0;
}

 

Answer: B

Correct answer: 4,5

Take the two operations in order. var starts at 0x04, and OR with 0x04 sets a bit that is already set, so nothing changes and 4 is printed. OR is idempotent: \( x \mathbin{|} x = x \).

Then var |= 0x01 sets bit 0, giving \( 0100_2 \mathbin{|} 0001_2 = 0101_2 = 5 \). The distractor 8 comes from reading | as addition, which coincides only when the operands have no bits in common - and here they overlap completely.

Please log in or register to report this question.

80.

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.

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