ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
unsigned alt(unsigned x) {
    unsigned y = x ^ (x >> 1);
    return (y & (y + 1)) == 0;
}
int main(void) {
    printf("%u %u", alt(10), alt(12));
}
  1. A. 0 1
  2. B. 1 1
  3. C. 1 0
  4. D. 0 0

Correct answer: C. 1 0

x ^ (x >> 1) for 1010 is 1111, which is all ones, so the test passes. For 1100 it is 1010, which fails.

More C Programming questions

Exams that ask this topic