ProgrammingHardProduct company OA
What is printed?
What is printed?
int f(unsigned x) {
int c = 0;
while (x) { c += x & 1; x >>= 2; }
return c;
}
int main(void) { printf("%d", f(0xFF) + f(0xAA)); }- A. 0
- B. 4
- C. 8
- D. 12
Correct answer: B. 4
Shifting by 2 each time means only bits 0, 2, 4, 6 … are ever tested. 0xFF has all four of those set (4); 0xAA = 1010 1010 has ones only in odd positions (0). '12' counts every set bit of both.
More C Programming questions
- What is printed? c #define VAR(n) v##n #define PRINT(n) printf("%d ", VAR(n)) int v1 = 10, v2 = 20, v12 = 30; PRINT(1); PRINT(1 + 1)…
- Assume 32-bit unsigned int. What is printed? c unsigned int n = 5; int count = 0; for (unsigned int i = n; i < n + 3; i -= 2) count++…
- What is printed? c char w[][6] = {"red", "green", "blue"}; printf("%zu %zu %zu", sizeof(w), sizeof(w[1]), strlen(w[1]));
- What is printed? c int m = 0; long p(long b, int e) { if (e == 0) return 1; long h = p(b, e / 2); m++; if (e % 2) { m++; return h h b; }…
- What is printed? c enum Flag { RED = 1, GREEN = RED << 1, BLUE = GREEN << 1, ALL = RED | GREEN | BLUE }; printf("%d %d", ALL, ALL & ~GREEN);
- What is printed? c void rev(char s, int i, int j) { if (i >= j) return; char t = s[i]; s[i] = s[j]; s[j] = t; rev(s, i + 2, j - 2); } int…