ProgrammingHardIBM · Deloitte pattern
Assume 32-bit int. What is printed?
Assume 32-bit int. What is printed?
int count = 0;
unsigned int x = 0xF0F0F0F0;
while (x) {
count += x & 1;
x >>= 1;
}
printf("%d", count);- A. 16
- B. 8
- C. 32
- D. 4
Correct answer: A. 16
This loop checks and discards the lowest bit one at a time (x & 1, then x >>= 1), counting the number of 1 bits (popcount) in 0xF0F0F0F0. Each hex digit F (1111) contributes 4 set bits, and there are 4 F digits, giving 4*4 = 16 set bits total.
More C Programming questions
- Assume 32-bit int. What is printed? c int x = 5; int y = x & -x; printf("%d", y);
- Assume 32-bit int. What is printed? c unsigned char a = 200; unsigned char b = 100; unsigned char c = a + b; printf("%u", c);
- Assume 32-bit int. What is printed? c int flags = 0; flags |= (1 << 2); flags |= (1 << 5); flags &= ~(1 << 2); printf("%d", flags);
- Assume 32-bit int. What is printed? c int a = -1; unsigned int b = a; printf("%u", b);
- What is printed? (Assume no overflow trapping.) c int f(int n) { if (n == 0) return 0; return n + f(n - 1); } int main(void) {…
- What is printed? c int mystery(int a, int b) { if (b == 0) return a; return mystery(b, a % b); } int main(void) { printf("%d", mystery(48…