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

Bitwise Operators Questions and Answers

1.

What is printed by the following ANSI C program?

#include\(\lt\)stdio.h\(\gt\)
int main(int argc, char *argv[]){
  char a = 'P';
  char b = 'x';
  char c = (a & b) + '*';
  char d = (a | b) - '-';
  char e = (a ^ b) + '+';
  printf("%c %c %c\n", c, d, e);
  return 0;
}

ASCII encoding for relevant characters is given below

ABC...Z
656667...90
abc...z
979899...122
*+-
424345

Exam Year(s)

GATE CS - 2022,

Answer: A

The program prints z K S.

'P' is 80 = 0101 0000 and 'x' is 120 = 0111 1000 in binary.

  • a & b = 0101 0000 = 80; adding '*' (42) gives 122, which is 'z'.
  • a | b = 0111 1000 = 120; subtracting '-' (45) gives 75, which is 'K'.
  • a ^ b = 0010 1000 = 40; adding '+' (43) gives 83, which is 'S'.

The format %c prints characters, not numbers, so '122 75 83' is the right values in the wrong form. '* - +' and 'P x +' ignore the bitwise operations and simply echo the operand characters.

Please log in or register to report this question.

2.

What will be the output of the program?

#include <stdio.h>

int main()
{
    unsigned char c = 0xAB;
    printf("%d", c & 0x0F);
    return 0;
}

Answer: B

Correct answer: 11

0xAB is 10101011 in binary, and 0x0F is 00001111. A mask of four low ones ANDed against a value keeps that value’s low four bits and zeroes everything above them:

  0xAB = 1 0 1 0 1 0 1 1
  0x0F = 0 0 0 0 1 1 1 1
  ---------------------
     & = 0 0 0 0 1 0 1 1

The result 00001011 is 0x0B, or 11 in decimal.

The shortcut worth learning is that one hexadecimal digit is exactly four bits, so masking with 0x0F simply keeps the last hex digit - the B of 0xAB, which is 11. By the same reasoning c >> 4 would give the other digit, 10. The distractor 171 is 0xAB itself, unmasked.

Please log in or register to report this question.

3.

Bitwise | can be used to set multiple bits in number.

Answer: A

Correct answer: Yes

OR with a mask forces every position named by the mask to 1 and leaves the others as they were, and a mask may name any number of positions at once.

So item |= MOBILE | LAPPY sets both flags in a single operation. This is why OR is the natural operator for accumulating flags: each |= adds to the set without disturbing what is already there, and the operation is idempotent, so setting a bit that is already on changes nothing.

Please log in or register to report this question.

4.

If an unsigned int is 2 bytes wide then, What will be the output of the program ?

#include <stdio.h>

int main()
{
    unsigned int m = 32;
    printf("%x\n", ~m);
    return 0;
}

 

Answer: C

Correct answer: ffdf

Write 32 in the given width first:

\[ 32 = 0000\,0000\,0010\,0000_2 = \mathtt{0x0020} \]

~ inverts every bit:

\[ 1111\,1111\,1101\,1111_2 = \mathtt{0xffdf} \]

The quick check is that inverting a hex digit means subtracting it from f: 0 becomes f, 0 becomes f, 2 becomes d, 0 becomes f. The distractor ffff would be ~0, and the width matters - on a 4-byte int the answer would be ffffffdf.

Please log in or register to report this question.

5.

The bitwise complement operator ~ is a unary operator.

Answer: B

Correct answer: True

The complement operator takes a single operand and inverts every one of its bits. The other bitwise operators - AND, OR, XOR and the two shifts - are all binary, taking an operand on each side.

Please log in or register to report this question.

6.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        int a = 2;
        if (a >> 1)
           printf("%d\n", a);
    }

 

Answer: B

Correct answer: 2

a >> 1 computes a value; it does not modify a. Only the compound form a >>= 1 would assign back.

So the test evaluates \( 2 \gg 1 = 1 \), which is non-zero and therefore true, and the printf then prints a - still 2, exactly as it was declared. The distractor 1 is the value of the shift expression, which is tested but never stored, and "No Output" would require the test to be false.

Please log in or register to report this question.

7.

Bitwise | can be used to set a bit in number.

Answer: A

Correct answer: Yes

Setting a single bit is n |= (1 << k). The shift builds a mask with one 1 in position \( k \); OR forces that position to 1 and leaves every other bit unchanged, since \( b \mathbin{|} 0 = b \).

The three basic single-bit operations follow the same shape and differ only in the operator: OR with the mask to set, AND with its complement to clear, and XOR with the mask to toggle. Testing uses AND with the mask without storing the result.

Please log in or register to report this question.

8.

In a two’s complement system, what is the value of ~5 ?

Answer: D

Correct answer: -6

Complementing inverts every bit, and in two’s complement that is equivalent to \(\sim n = -(n+1)\).

\[\sim 5 = -(5+1) = -6\]

Checking with 8 bits: 5 is 00000101, its complement is 11111010, and that pattern read as a two’s complement number is -6.

Please log in or register to report this question.

9.

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.

10.

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.

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