which means "return 32; "In c?

1

This is the exercise, I just wanted to know what you mean by:

  

return 32;

 #include <stdio.h>
#include <stdlib.h>

int bits(unsigned int n);
int main()
{
    unsigned int n=7,j;
    j=bits(n);
    printf("%d",j);

    return 0;
}

int bits(unsigned int n)
{
    if(n==0)
    {
        return 32;
    }
        else
        {
            if(n&1==1)
            {
                return 1+bits(n>>1);
                }
            else
            {
                return bits(n>>1);
            }
        }

}
    
asked by Emmaaaaa 27.10.2017 в 23:38
source

1 answer

0

The bits function delivers the number of bits in 1; that contains the binary representation of the number n .

In this case, it is a recursive function that tells them. It can be noted that it effectively counts the bits in 1, given the iterative case. However, the base case should deliver a 0. This given that 32 is the case that all the bits are 1; and that should only happen when n is the maximum unsigned int.

If it were you, I would try the function with different values of n, and comparing them with their expected values. If you give wrong values, I would change the 32 to a 0 and check again. Surely it is a mistake.

Greetings,

    
answered by 12.11.2017 / 20:14
source