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

Bitwise Operators Questions and Answers

51.

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.

52.

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.

53.

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.

54.

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.

55.

What is the value of the expression 13 | 25?

Answer: B

Correct answer: 29

OR compares the two numbers bit by bit and keeps a 1 where either has one:

  13 = 0 1 1 0 1
  25 = 1 1 0 0 1
  --------------
   |  = 1 1 1 0 1

The result 11101 is \( 16 + 8 + 4 + 1 = 29 \).

A useful check: OR can never produce a value smaller than either operand, since it only ever turns bits on, so an answer below 25 would be wrong on inspection alone. That rules out 9 - which is what & gives - and 20, which is ^, without any arithmetic.

Please log in or register to report this question.

56.

What is the value of the expression 13 ^ 25?

Answer: A

Correct answer: 20

XOR keeps a 1 exactly where the two numbers differ:

  13 = 0 1 1 0 1
  25 = 1 1 0 0 1
  --------------
   ^  = 1 0 1 0 0

The first bits differ, so 1; the second are both 1, so 0; the third differ, so 1; the fourth are both 0, so 0; the fifth are both 1, so 0. The result 10100 is \( 16 + 4 = 20 \).

Two properties of XOR are worth carrying away. Applying it twice restores the original - \( (a \oplus b) \oplus b = a \) - which is why it appears in simple ciphers and in the swap-without-a-temporary trick. And \( a \oplus a = 0 \) for any value, which makes XOR the standard way to find the one unpaired element in a list where everything else appears twice.

Please log in or register to report this question.

57.

If unsigned char c = 0xF0;, what is the value of c >> 4?

Answer: C

Correct answer: 15

The literal 0xF0 is 11110000 in binary - the high four bits of the byte set, the low four clear. Shifting right by four moves every bit four places towards the least significant end, so those four ones land in the low nibble and the vacated positions are filled with zeros: 00001111, which is \( 8+4+2+1 = 15 \), or 0x0F.

The shorthand is that a right shift by \( n \) divides by \( 2^{n} \), and here \( 240 / 16 = 15 \).

The type matters. Because c is unsigned, the vacated high bits are guaranteed to be filled with zeros. On a signed type holding a negative value the standard leaves the fill implementation-defined, and most compilers propagate the sign bit instead - which is why bit manipulation is normally done on unsigned types. The distractor 240 is the value of c itself, left unshifted.

Please log in or register to report this question.

58.

Assuming a integer 2-bytes, What will be the output of the program?

#include <stdio.h>

int main()
{
    printf("%x\n", -1<<3);
    return 0;
}

 

Answer: B

Correct answer: fff8

On a 2-byte int, \( -1 \) is all ones: 0xffff. Shifting left by three discards the top three bits and brings three zeros in at the bottom:

\[ 1111\,1111\,1111\,1111_2 \ll 3 = 1111\,1111\,1111\,1000_2 = \mathtt{0xfff8} \]

The quick check is that the low three bits become zero and nothing else changes, so only the last hex digit moves, from f to 8. Note that left-shifting a negative value is undefined behaviour in C; the result shown is what implementations produce, not what the standard guarantees.

Please log in or register to report this question.

59.

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.

60.

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.

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