ProgrammingHardProduct-company MCQ pattern
Assume 32-bit int and two's complement. What is printed?
Assume 32-bit int and two's complement. What is printed?
int x = 5;
int y = ~x + 1;
printf("%d", y);- A. -6
- B. 5
- C. -5
- D. 6
Correct answer: C. -5
~x flips every bit of 5 (0...0101), giving two's-complement -6 (0...1010 is -6's bit pattern). Adding 1 to -6 gives -5. This is exactly the two's-complement identity -x == ~x + 1, so y = -5 = -x.
More C Programming questions
- Assume 32-bit int. What is printed? c int x = 12; printf("%d %d %d", x & 1, x | 1, x ^ 1);
- Assume 32-bit int. What is printed? c int a = 6, b = 3; printf("%d %d %d %d", a & b, a | b, a ^ b, ~a);
- Assume 32-bit int. What is the value of n that checks whether the k-th bit (0-indexed from the LSB) of x is set, for x=44 and k=3, and…
- Assume 32-bit int. What does this snippet do, and what is printed for x=10? c int x = 10; int y = x & (x - 1); printf("%d", y);
- Assume 32-bit int. What is printed? c #define TOGGLE(flags, bit) ((flags) ^= (1 << (bit))) int flags = 0b0110; TOGGLE(flags, 1)…
- Assume 32-bit int. What is printed? c int count = 0; unsigned int x = 0xF0F0F0F0; while (x) { count += x & 1; x >>= 1; } printf("%d"…