ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int f(unsigned x) {
int c = 0;
while (x & (x >> 1)) { x &= x >> 1; c++; }
return c + (x != 0);
}
int main(void) { printf("%d %d", f(0xEE), f(0x7F0F)); }- A. 6 11
- B. 3 4
- C. 2 6
- D. 3 7
Correct answer: D. 3 7
x &= x >> 1 shortens every run of consecutive 1s by one bit, so the loop counts how many times this can happen before no two adjacent 1s remain; adding 1 for the bits left gives the longest run of 1s. 0xEE = 1110 1110 → 3; 0x7F0F = 0111 1111 0000 1111 → 7. '6 11' counts all set bits.
More C Programming questions
- What is printed? c char d[6] = "zzzzz"; strncpy(d, "abc", 2); printf("%s", d);
- Assume ASCII. What is printed? c char s[] = "xyzAB"; for (char p = s; p; p++) if (p >= 'a' && p <= 'z') p = 'a' + (p - 'a' + 3) % 26…
- What is printed? c int up(int a, int b) { return (a + b - 1) / b; } int main(void) { printf("%d %d %d", up(10, 3), up(9, 3), up(0, 3)); }
- What is printed? c int t = 0; for (unsigned i = 0; i < 16; i++) for (unsigned x = i; x; x &= x - 1) t++; printf("%d", t);
- What is printed? c void s1(int a, int b) { int t = a; a = b; b = t; } void s2(int a, int b) { int t = a; a = b; b = t; } int main(void) {…
- Why does this fail to compile in C? c void f(int n) { static int k = n; }