ProgrammingVery hardProduct company OA
What is printed?
What is printed?
unsigned x = 8;
if (x & (x - 1) == 0) puts("pow2");
else puts("not");- A. pow2
- B. Nothing
- C. not
- D. Compilation error
Correct answer: C. not
== binds tighter than &, so the test is x & ((x − 1) == 0) = 8 & 0 = 0, which is false, even though 8 is a power of two. The intended test needs parentheses: (x & (x − 1)) == 0.
More C Programming questions
- What is printed? c int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, r[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++)…
- What is printed? c const char s = "banana", p = s; int c = 0; while ((p = strchr(p, 'a')) != NULL) { c++; p++; } printf("%d", c);
- Assume IEEE-754 double. What is printed? c printf("%d %d", 0.1 + 0.2 == 0.3, 0.5 + 0.25 == 0.75);
- Assume 4-byte int. What is printed? c printf("%d %d", sizeof(int) -1 < 0, (int)sizeof(int) -1 < 0);
- What is printed? c Bool b = 0.5; printf("%d %d", b + 2, (Bool)256);
- What is printed? c int c = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { if (j == 1) break; c++; } printf("%d", c);