ProgrammingHardProduct company OA
Assume 32-bit unsigned int. What is printed?
Assume 32-bit unsigned int. What is printed?
unsigned mask(int lo, int hi) { return ((1u << (hi - lo + 1)) - 1) << lo; }
int main(void) { printf("%X %X", mask(4, 7), mask(0, 3) | mask(8, 11)); }- A. F0 F0F
- B. F 0F0F
- C. 70 F0F
- D. F0 FFF
Correct answer: A. F0 F0F
(1 << k) − 1 is a run of k ones; shifting it left by lo places the run at bits lo to hi. mask(4, 7) = 0xF0. mask(0, 3) = 0x00F and mask(8, 11) = 0xF00 combine to 0xF0F; 'FFF' wrongly fills bits 4 to 7 too.
More C Programming questions
- What is printed? c int f(unsigned x) { int c = 0; while (x & (x >> 1)) { x &= x >> 1; c++; } return c + (x != 0); } int main(void) {…
- 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) {…