ProgrammingHardTCS NQT · Wipro
How many integers in 0..15 are a power of two? Compute with this program.
How many integers in 0..15 are a power of two? Compute with this program.
#include <stdio.h>
int main(void) {
int c = 0;
for (unsigned x = 0; x < 16; x++)
if (x && !(x & (x - 1))) c++;
printf("%d", c);
}- A. 3
- B. 6
- C. 4
- D. 5
Correct answer: C. 4
The powers of two in range are 1, 2, 4 and 8, and the check x && !(x & (x - 1)) excludes 0. Counting 0 as a power of two gives 5.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int a[] = {3, 1, 2}, swaps = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 2…
- What is printed? Assume uint32t is 32 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint32t h = 5381; const char s =…
- What is printed? c #include <stdio.h> #include <stdlib.h> struct R { int a, b; }; int cmp(const void p, const void q) { const struct R x =…
- How many values of n in 1..50 have bit 2 set (the value 4)? Compute with this program. c #include <stdio.h> int main(void) { int c = 0…
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { int m[4][5]; printf("%zu %zu", sizeof m / sizeof m[0]…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned s = 0; for (unsigned i = 1; i <= 100000…