ProgrammingHardTCS NQT · Infosys
What is printed? The program computes the parity of the set bits of 0b1101.
What is printed? The program computes the parity of the set bits of 0b1101.
#include <stdio.h>
int parity(unsigned x) {
return x == 0 ? 0 : (int)(x & 1u) ^ parity(x >> 1);
}
int main(void) {
printf("%d", parity(0xD));
}- A. 0
- B. 3
- C. 1
- D. 2
Correct answer: C. 1
0b1101 has three set bits, and the recursive XOR of the low bits gives 1 for an odd count.
More C Programming questions
- What is the position of the lowest set bit of 0x0300 (counting bit 0 as position 0)? Compute with this program. c #include <stdio.h> int…
- What is printed? The program inverts the Gray code 6 by XOR-folding its bits. c #include <stdio.h> int main(void) { unsigned g = 6, n = 0…
- What is printed? c #include <stdio.h> int main(void) { unsigned char c = 200; if (c 2 > 300) puts("big"); else puts("small"); }
- What is the difference between the largest and the smallest element of {4, -2, 9, 0}, found in a single pass with pointers? Compute it…
- How many pairs (i, j) with i < j in {1, 3, 5, 8, 10} have a difference of exactly 2 (that is, a[j] - a[i] = 2)? Compute with this program…
- Starting at node 0, how many nodes does this linked walk visit before it reaches the sentinel -1? The next-index array is {3, 2, 0, -1}. c…