ProgrammingModerateCompetitive coding (CP)
What is printed?
What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
uint32_t x = ~0u >> 3;
int c = 0;
for (; x; x &= x - 1) c++;
printf("%d", c);
}- A. 28
- B. 30
- C. 32
- D. 29
Correct answer: D. 29
~0u is 32 ones, and shifting right by three leaves 29 ones in the low bits. The loop counts exactly those set bits.
More C Programming questions
- What is printed? c #include <stdio.h> unsigned long long g(int n) { if (n < 2) return 0; return g(n - 1) + g(n - 2) + 1; } int main(void)…
- int c = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < i i; j++) c++; printf("%d", c); What is printed?
- int c = 0; for (int i = 1; i <= 12; i++) c += 12 / i; printf("%d", c); What is printed?
- int c = 0; for (int i = 1; i <= 10; i++) for (int j = i; j <= 10; j += i) c++; printf("%d", c); What is printed?
- int c = 0; for (int i = 1; i i <= 50; i++) c++; printf("%d", c); What is printed?
- int c = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { if (i == j) continue; c++; } printf("%d", c); What is printed?