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));- A. 1 1 240 224 1
- B. 0 1 -16 224 4
- C. 0 1 -16 480 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
- Assume 32-bit int and 64-bit long long. What is printed? c int n = -1; unsigned int len = 3; long long big = -1; printf("%d %d %d %d", n <…
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed? c struct A { char c; double d; int…
- What is printed? c #define SQ(x) x x #define MIN(a, b) ((a) < (b) ? (a) : (b)) int calls = 0; int next(void) { return ++calls; } int…
- What is printed? c #define N 4 #define STR(x) #x #define XSTR(x) STR(x) #define CAT(a, b) a##b #define XCAT(a, b) CAT(a, b) int NN = 9…
- Which of the numbered statements violate a constraint (i.e. must be rejected by the compiler)? c typedef char strt; #define STRM char char…
- What is printed? c int g = 1; int f(int n) { static int s = 10; int g = n; s += g; { extern int g; g += s; } return s; } int main(void) {…