ProgrammingVery hardCompetitive coding (CP)

How many of these statements are true for unsigned x = 5? Print the count from this program.

How many of these statements are true for unsigned x = 5? Print the count from this program.
#include <stdio.h>
int main(void) {
    unsigned x = 5;
    int c = 0;
    if ((x & (x - 1)) == 4) c++;
    if ((x & (0u - x)) == 1) c++;
    if ((unsigned)(~x + 1u) == (0u - x)) c++;
    if (x % 4 == 1) c++;
    printf("%d", c);
}
  1. A. 2
  2. B. 4
  3. C. 3
  4. D. 1

Correct answer: B. 4

All four tests hold: 5 & 4 = 4, 5 & -5 isolates the lowest bit 1, ~x + 1 equals 0u - x in unsigned arithmetic, and 5 % 4 = 1, so the count is 4.

More C Programming questions

Exams that ask this topic