recursive function which will return number of binary ones from the given number

omkarlanghe

I have written recursive function which will return me the count of number of one bits from a given number. Whenever I try to run the code, it says segmentation fault core dump. Please tell me what’s going wrong in this code??

#include<stdio.h>
int main(){
    int no;
    printf("Enter the number:\n");
    scanf("%d",&no);
    printf("Count of Number of One Bits is:%d\n",recursiveCountNumberOfOnes(no));
    //recursiveCountNumberOfOnes(no);   
}

int recursiveCountNumberOfOnes(int no){
int x=1;
int count=0;

    if((no&x)!=0)
    {
        count++;
        x=x<<1; 
    }
    return recursiveCountNumberOfOnes(count);

}
cleblanc

Here's a simple way to do it for unsigned integers. Notice how it just returns 1 + each time a bit is set, and continues to recurse as long as no is not zero. Finally adding in a zero at the end.

int recursiveCountNumberOfOnes(unsigned int no)
{
    if (no&1)
        return 1 + recursiveCountNumberOfOnes(no>>1);
    else if (no)
        return recursiveCountNumberOfOnes(no>>1);
    else
        return 0;    
}

And a slightly golfier version

int recursiveCountNumberOfOnes(unsigned n) {
    return n?(n&1)+recursiveCountNumberOfOnes(n/2):0;    
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Given a string which contains only ones and zeros, return the number of substrings with ones more than zeros

Having function choose largest number of given ones

Return all matrices of a given dimension with a certain number of ones and remaining zeros

Return contiguous 0's in a given binary number

Recursive function to check if a given number is Fibonacci

Return an answer depending on which number is given (if statement)

Get the the number of zeros and ones of a binary number in Python

Binary random number with a specific number of ones

Python: Given a number return tuple from 0, a number given

return binary number from Python dictionary values

Write a function which checks if a number is a square with recursive function

Given a binary array return a count the number of consecutive 1's

Prime Number between given interval using Recursive Function

recursive Function, to find even or odd digits inside given number

Find number of ‘+’ formed by all ones in a binary matrix

Binary search recursive number of calls?

How to return the largest number returning from a function in which the Object.values(obj) is used in it to return an array?

Javascript: modifying a function which prints out variable number of arguments to be recursive?

Return true if a binary number either contains all zero or all ones on a single flip.(Python)

Function which get number from array and set this number to other function

Why does recursive function "infinite" loop return same number

check given number is binary or not in c

Is there a function which keeps increasing a number in a matrix in given order

Function to return items from an array where items at odd index which are repeated by the number at its preceding even index

return a recursive closure which move environmental mutable variable into it from a function?

R: how to create a binary matrix with columns from largest number of ones to least

Haskell function to return all powers of a number until a given maximum

Algorithm to get which values make sum of a given number from array

Selecting all the records from table to which given number belongs to