ProgrammingHardProduct company OA
What is printed?
What is printed?
int h(unsigned a, unsigned b) {
int c = 0;
for (unsigned x = a ^ b; x; x >>= 1) c += x & 1;
return c;
}
int main(void) { printf("%d %d", h(29, 15), h(0xFF, 0x100)); }- A. 3 9
- B. 2 9
- C. 2 1
- D. 4 8
Correct answer: B. 2 9
a ^ b has a 1 wherever the two numbers differ, and the loop counts those 1s (the Hamming distance). 29 = 11101 and 15 = 01111 differ in 2 places. 0xFF ^ 0x100 = 0x1FF, nine 1s. '2 1' assumes 0xFF and 0x100 differ in one bit.
More C Programming questions
- What is printed? c int a[] = {10, 20, 30, 40, 50}; int p = &a[3]; printf("%d %d %d", p[-2], -1[p], (p - 1)[-1]);
- What is printed? c struct C { int r, g, b; } c = {.b = 9, .r = 4}; printf("%d %d %d", c.r, c.g, c.b);
- Assume 4-byte int with 4-byte alignment. What is printed? c union U { char c[5]; int i; }; printf("%zu", sizeof(union U));
- Compiled as C99 or later, what is printed? c int i = 1; for (int i = 5; i < 7; i++) { int i = 10; printf("%d ", i); } printf("%d", i);
- What is printed? c printf("%zu", sizeof("1234"));
- What is printed? c int a[8] = {0}; for (int i = 1; i < 8; i++) a[i] = a[i / 2] + (i % 2); printf("%d %d %d", a[5], a[6], a[7]);