ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int alt(unsigned x) {
unsigned y = x ^ (x >> 1);
return (y & (y + 1)) == 0;
}
int main(void) { printf("%d%d%d", alt(5), alt(10), alt(11)); }- A. 100
- B. 010
- C. 110
- D. 111
Correct answer: C. 110
If x's bits alternate, x ^ (x >> 1) is a solid block of 1s from bit 0 up, and a value of the form 2^k − 1 satisfies y & (y + 1) == 0. 5 = 101 and 10 = 1010 alternate; 11 = 1011 gives y = 1110, which fails.
More C Programming questions
- What is printed? c struct In { int a[2]; }; struct Out { struct In in[2]; int k; } o = {{{{1, 2}}, {{3, 4}}}, 5}; printf("%d %d"…
- What is printed? c struct In { int a[2]; }; struct Out { struct In in[2]; int k; } q = {1, 2, 3, 4, 5}; printf("%d %d", q.in[1].a[1], q.k);
- What is printed? c int c = 0; int f(int n) { c++; return n <= 1 ? 1 : f(n / 2) + f(n - n / 2); } int main(void) { int r = f(7); printf("%d…
- Which of these loops run their body exactly for i = 4, 3, 2, 1, 0 (once each) with an unsigned i? c for (unsigned i = 5; i-- > 0; ) { /…
- What is printed? c int x = -1; unsigned y = 1; printf("%d", (x < 0 ? x : y) < 0);
- What is printed? c int a[] = {1, 2, 3, 4, 5, 6}; int s = 0; for (int p = a; p < a + 6; p += 2) s += p p[1]; printf("%d", s);