ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
unsigned int x = 10;
printf("%d", (x << 1) == (x * 2));
printf(" %d", (x >> 1) == (x / 2));- A. 1 0
- B. 1 1
- C. 0 1
- D. 0 0
Correct answer: B. 1 1
For a non-negative value with no overflow, left-shift by 1 is equivalent to multiplying by 2 (10<<1=20=10*2), and right-shift by 1 on an unsigned value is equivalent to integer division by 2 (10>>1=5=10/2). Both comparisons are true.
More C Programming questions
- Assume 32-bit int, two's complement. What is printed? c int x = ~0; printf("%d %u", x, (unsigned int)x);
- Assume 32-bit int. What is printed? c int x = 13; int count = 0; while (x) { count += x & 1; x >>= 1; } printf("%d", count);
- Assume 32-bit int. What is printed? c int x = -5; printf("%d", x & 1);
- Assume 32-bit int, two's complement. What is printed? c int x = 7; int y = -7; printf("%d %d", x | y, x ^ y);
- What is printed? c int a = 0; for (int i = 0; i < 4; i++) { a |= (1 << i); } printf("%d", a);
- What is printed? c int x = 255; unsigned char y = (unsigned char)(x << 1); printf("%d", y);