#include<stdio.h>
#include<math.h>

int main()
{
        int num,i,count0=0,count1=0;
        printf("enter number \n");
        scanf("%d",&num);
        for(i=0;i<32;i++)
        {
                if(num &((pow(2,31))>>i))
                        count1++;
                else
                        count0++;
        }
        printf(" the no. of 0's and 1's in the given number are %d %d",count0,count1);
}

Recommended Answers

All 3 Replies

The result of "pow(2,31)" is a double. You apply a bitshift operation on it, which is not possible. Replace

pow(2,31)

with

((unsigned int)pow(2,31))

and it should work.

You don't need pow to begin with though, you don't have to start from the most significant digit as you're just counting bits. You could therefore also replace

if(num &((pow(2,31))>>i))

with

if(num & (1 << i))

You acn't use bitwise operator to double or float.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.