What does the following C function f() return?
int f(unsigned int N) {
unsigned int counter = 0;
while (N > 0) {
counter += N & 1;
N = N >> 1;
}
return counter == 1;
}
Pick an option to check yourself, or open the answer below.
Show answer & explanation
Correct answer: B. 1 if N is a power of 2 , otherwise 0
Explanation
The loop counts the 1 bits of N, and the function returns whether that count is exactly 1 — true exactly when N is a power of 2.
More Programming Languages and Computer Graphics questions
- Consider the following recursive Java function: public static String F(int N) { if (N <= 0) return "-"; return F(N - 3) + N + F(N - 2) + N;…
- Match List I (programming terms) with List II (meaning). List I: A. JNDI, B. RMI, C. JDK, D. JRE. List II: I. Runtime support for running J…
- Match List I (programming paradigms) with List II (characteristics). List I: A. Imperative, B. Object-oriented, C. Logic, D. Functional. Li…
- Suppose you have eight black-and-white images taken with a 1-megapixel camera and one 8-colour image taken with an 8-megapixel camera. How…
- Which of the statements given below are correct? The midpoint (or Bresenham) algorithm for rasterizing lines is optimized relative to DDA a…
- What is the transformation matrix that transforms the square with vertices (1, 1), (−1, 1), (−1, −1), (1, −1) into the parallelogram with c…