ProgrammingVery hardProduct company OA

Assume 32-bit int and two's complement. What is printed?

Assume 32-bit int and two's complement. What is printed?
unsigned char a = 0x0F;
unsigned char b = ~a;
printf("%d %d %d %d %zu", ~a == b, (unsigned char)~a == b, ~a, (unsigned char)(a << 5), sizeof(a + a));
  1. A. 1 1 240 224 1
  2. B. 0 1 -16 224 4
  3. C. 0 1 -16 480 4
  4. D. 1 1 -16 224 4

Correct answer: B. 0 1 -16 224 4

Before ~, << or + an unsigned char is promoted to int. ~a is therefore ~0x0000000F = −16, not 0xF0. Storing it in b truncates to 240, so the comparison ~a == b is −16 == 240 → 0; casting back to unsigned char first makes it 1. a << 5 = 0x1E0 as an int, truncated to 0xE0 = 224. sizeof(a + a) is sizeof(int) = 4 for the same reason. Every '1 1 …' option assumes ~ works in 8 bits.

More C Programming questions

Exams that ask this topic