Top | MCQs on Bitwise Algorithms and Bit Manipulations with Answers | Question 30

Last Updated :
Discuss
Comments

What is the output of the below code for input num = 15?

C++
unsigned int fun(unsigned int n)
{
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
C
unsigned int fun(unsigned int n) {
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
Java
public class Main {
    public static int fun(int n) {
        int count = 0;
        while (n != 0) {
            count += n & 1;
            n >>= 1;
        }
        return count;
    }
}
Python
def fun(n):
    count = 0
    while n:
        count += n & 1
        n >>= 1
    return count
JavaScript
function fun(n) {
    let count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}

15

5

4

3

Share your thoughts in the comments