ProgrammingHardStatement-count style · Product-company MCQ pattern

For a signed int n on a two's-complement machine, how many of these are correct? (i) (n & (n - 1)) == 0 is true for every positive power of two. (ii) (n & (n - 1)) == 0 is false for n = 0. (iii) n & -n isolates the highest set bit of a non-zero n (other than INT_MIN). (iv) n >> 1 equals n / 2 for every negative n.

  1. A. Only one
  2. B. Only two
  3. C. Only three
  4. D. All four

Correct answer: A. Only one

Only (i) is correct. (ii) is false: 0 & -1 = 0, so the test is true for 0 and needs n > 0 as well. (iii) is false: n & -n isolates the lowest set bit. (iv) is false: -7 >> 1 is -4 with arithmetic shift, while -7 / 2 = -3.

More C Programming questions

Exams that ask this topic