ProgrammingHardGATE CS · Competitive coding (CP)
How many integers n with 1 <= n <= 64 satisfy both (n & (n - 1)) == 0 and (n & 0x55555555u) != 0? Compute with this program.
How many integers n with 1 <= n <= 64 satisfy both (n & (n - 1)) == 0 and (n & 0x55555555u) != 0? Compute with this program.
#include <stdio.h>
int main(void) {
int c = 0;
for (unsigned n = 1; n <= 64; n++)
if ((n & (n - 1)) == 0 && (n & 0x55555555u) != 0) c++;
printf("%d", c);
}- A. 3
- B. 5
- C. 6
- D. 4
Correct answer: D. 4
The first test keeps the powers of two, and the mask keeps those whose only set bit is at an even position. The qualifying values are 1, 4, 16 and 64, which gives 4.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int a[5]; int p = a + 4, q = a + 1; printf("%ld", (long)(p - q) (long)sizeof p); }
- What is printed? Assume int is 32 bits and integer division truncates toward zero. c #include <stdio.h> int main(void) { int v[] = {-7…
- What is the maximum value of depth printed? c #include <stdio.h> int depth = 0, maxd = 0; void r(int n) { depth++; if (depth > maxd) maxd…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3, 4, 5}; int p = a + 2; printf("%d %d", p[-1], p[2]); }
- What is printed? Assume uint32t is 32 bits. c #include <stdint.h> #include <stdio.h> uint32t rotl(uint32t x, unsigned k) { return (x << k)…
- What is printed? c #include <stdio.h> int main(void) { unsigned u = 1; int s = -2; printf("%d %d", s < u, s + (int)u > -2); }