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);
}- A. 2
- B. 4
- C. 3
- 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
- What is printed? c #include <stdio.h> int main(void) { int a[] = {5, 6, 7}, p = a + 1; printf("%d", p[-1] + p + p[1]); }
- How many tilings does a 2 by 5 board have with 2 by 1 dominoes? Compute with this recursive program. c #include <stdio.h> int f(int n) {…
- What is printed? c #include <stdio.h> #include <stdlib.h> int v[] = {30, 10, 20}; int cmp(const void a, const void b) { int i = (const int…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { unsigned x = (const unsigned )a, y =…
- How many numbers from 1 to 100 have bit 3 set (value 8)? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? Assume uint16t is 16 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint16t x = 60000; uint16t y = x 2…