ProgrammingHardWipro · Cognizant pattern
Assume 32-bit int. What is printed?
Assume 32-bit int. What is printed?
int x = 13;
int count = 0;
while (x) {
count += x & 1;
x >>= 1;
}
printf("%d", count);- A. 4
- B. 3
- C. 13
- D. 2
Correct answer: B. 3
13 in binary is 1101, which has three set bits. The loop isolates and counts the lowest bit each iteration while right-shifting, so count ends at 3.
More C Programming questions
- 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);
- What is printed? c int x = 5, y = 9; int max = y ^ ((x ^ y) & -(x < y)); printf("%d", max);
- What is printed? c int x = 101; int y = (x >> 2) << 2; printf("%d", y);