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.
- A. Only one
- B. Only two
- C. Only three
- 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
- What is printed? c #define DOUBLE(x) x + x printf("%d", 10 / DOUBLE(5) 2);
- Which of a, b, c, d are pointers? c #define IPTR int typedef int IPT; IPTR a, b; IPT c, d;
- What is printed? c #define DEBUG 0 ... #ifdef DEBUG printf("on "); #endif #if DEBUG printf("trace"); #endif
- In C, with int i = 1, a[5];, which statement has undefined behaviour?
- Compiled as C with 4-byte int, what is printed? c printf("%zu %zu", sizeof('A'), sizeof(char));
- What is printed? c int x = 1; { int x = 2; { x = 3; } printf("%d ", x); } printf("%d", x);