ProgrammingVery hardCompetitive coding (CP)
How many bit positions are zero in both 0xAAAA and 0x5555 within 16 bits? Count them with this program.
How many bit positions are zero in both 0xAAAA and 0x5555 within 16 bits? Count them with this program.
#include <stdint.h>
#include <stdio.h>
int main(void) {
uint16_t m = (uint16_t)~(0xAAAAu | 0x5555u);
int c = 0;
for (int i = 0; i < 16; i++)
if ((m >> i) & 1u) c++;
printf("%d", c);
}- A. 0
- B. 1
- C. 16
- D. 8
Correct answer: A. 0
0xAAAA and 0x5555 together set every bit of the 16-bit word, so their complement has no set bits and the count is 0.
More C Programming questions
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 12; printf("%u", (x << 3) + (x << 1)); }
- How many adjacent pairs of 1 bits does 27 (binary 11011) have? The program counts the set bits of x & (x >> 1). c #include <stdio.h> int…
- What is printed? c #include <stdio.h> int main(void) { int b[] = {1, 1, 0, 1, 1, 1, 0, 1, 1, 1}, run = 0, best = 0; for (int i = 0; i <…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3, 4, 5}; for (int i = 0; i + 1 < 5; i += 2) { int t = a[i]; a[i]…
- What is printed? c #include <stdio.h> int f(int n) { return n <= 1 ? 0 : 1 + f(n / 2); } int main(void) { printf("%d", f(10)); }
- What is printed? Assume uint16t is 16 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint16t a = 0xFFF0, b = 0x20…