ProgrammingHardGATE CS
What is the value printed? Assume unsigned int is 32 bits.
What is the value printed? Assume unsigned int is 32 bits.
#include <stdio.h>
int main(void) {
unsigned x = 0xB6D9 ^ 0x00FF;
int n = 0;
while (x) {
x &= x - 1;
n++;
}
printf("%d", n);
return 0;
}- A. 8
- B. 6
- C. 9
- D. 10
Correct answer: A. 8
x &= x - 1 clears the lowest set bit, so the loop counts the set bits of x. 0xB6D9 ^ 0x00FF = 0xB626 = 1011 0110 0010 0110 in binary, which has 3 + 2 + 1 + 2 = 8 ones (the original 0xB6D9 has 10).
More C Programming questions
- What is printed? c #include <stdio.h> #define MAX(a, b) ((a) > (b) ? (a) : (b)) int main(void) { int i = 3, j = 5; int m = MAX(i++, j++)…
- What is printed? c #include <stdio.h> int f(int n) { static int c = 0; c++; if (n >= 2) { f(n - 1); f(n - 2); } return c; } int main(void)…
- What is printed? Assume pointers are 8 bytes and the string literal's characters are those between the quotes. c #include <stdio.h>…
- What is the exact output? c #include <stdio.h> int main(void) { int n = printf("%d%d", 12, 345); printf("|%d", n); return 0; }
- What is printed? Assume int is 4 bytes and pointers are 8 bytes. c #include <stdio.h> void f(int a[10]) { printf("%zu ", sizeof a); } int…
- What is printed? Assume the usual ABI: char 1 byte, short 2, int 4, with each member aligned to its own size and the struct padded to a…