ProgrammingHardProduct company OA
Assume 16-bit unsigned short and 32-bit int. What is printed?
Assume 16-bit unsigned short and 32-bit int. What is printed?
unsigned short v = 0x1234;
unsigned short w = (unsigned short)(v << 8 | v >> 8);
printf("%04X", w);- A. 3412
- B. 123412
- C. 1234
- D. 3400
Correct answer: A. 3412
v is promoted to int, so v << 8 = 0x123400 and v >> 8 = 0x12; OR gives 0x123412. Casting back to 16 bits keeps the low half, 0x3412: the two bytes are swapped. Without the cast you would see the 0x12 hanging on top.
More C Programming questions
- What is printed? c void conv(int n, int b) { if (n >= b) conv(n / b, b); putchar("0123456789ABCDEF"[n % b]); } int main(void) { conv(255…
- A single C source file contains these three lines at file scope. What happens? c int x; int x; int x = 5;
- Assume 4-byte int, 8-byte double and natural alignment. What is printed? c struct T { double d; int i; }; struct T arr[3]; printf("%td"…
- What is printed? c int len(const char s) { return s ? 1 + len(s + 1) : 0; } int cnt(const char s, char c) { return !s ? 0 : (s == c) +…
- What is printed? c int dr(int n) { return n < 10 ? n : dr(n / 10 + n % 10); } int main(void) { printf("%d %d", dr(9875), dr(99999)); }
- What is printed? c unsigned x = 0xF0F0; x = x - ((x >> 1) & 0x5555); x = (x & 0x3333) + ((x >> 2) & 0x3333); x = (x + (x >> 4)) & 0x0F0F…