ProgrammingVery hardProduct company OA
What is printed?
What is printed?
unsigned next(unsigned x) {
unsigned c = x & -x, r = x + c;
return (((r ^ x) >> 2) / c) | r;
}
int main(void) { printf("%u %u", next(6), next(11)); }- A. 7 12
- B. 10 13
- C. 9 13
- D. 12 14
Correct answer: C. 9 13
This is Gosper's hack: it returns the next larger number with the same count of 1 bits. 6 = 0110 → 1001 = 9 (two bits set). 11 = 1011 → 1101 = 13 (three bits set). '12 14' just shifts or adds without keeping the bit count.
More C Programming questions
- What is printed? c const char s = "abcabcab"; const char p = strstr(s, "cab"); printf("%td %s %td", p - s, p + 3, strstr(p + 1, "cab") - s);
- What is printed? c struct A { int v[3]; }; struct A make(void) { struct A a = {{1, 2, 3}}; return a; } int main(void) { struct A x =…
- What is printed? c const char s = "a1b2c3d4"; int sum = 0; for (; s; s++) { if (s >= 'a') continue; if (s == '3') break; sum += s - '0'; }…
- What is printed? c int s = 0; for (int i = 0; i < 5; i++) { switch (i) { case 1: continue; case 3: break; default: s += 10; } s += i; }…
- What is printed? c int a[] = {10, 20, 30}; printf("%d", a[1, 2]);
- What is printed? c struct P { int x; int y; } p = {7, 9}; int q = (int )&p; printf("%d", q);