Skip to main content
deleted 4 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Counting number of 1's and 0's of Integer by usingfrom integer with bitwise operatoroperation

Is there a better way of doing this?

/*
/ Definition: Count number of 1's and 0's offrom Integerinteger with using bitwise operator.operation
//
// 2^32 = 4,294,967,296
// unsigned int 32 bit
*/

#include<stdio.h>

int CountOnesFromItegerCountOnesFromInteger(unsigned int);

int main()
{
    unsigned int inputValue;
    short unsigned int onesOfValue;
    printf("Please Enter value (between 0 to 4,294,967,295) : ");
    scanf("%u",&inputValue);
    onesOfValue = CountOnesFromInteger(inputValue);

    printf("\nThe Number has \"%d\" 1's and \"%d\" 0's",onesOfValue,32-onesOfValue);
}

int CountOnesFromInteger(unsigned int value)
{
    unsigned short int i, count = 0;
    for(i = 0; i < 32 ; i++)
    {
        if (value % 2 != 0)
        {
            count++;
        }
        value = value >> 1;
    }
    return count;
}

Counting 1's and 0's of Integer by using bitwise operator

Is there a better way of doing this?

/*
Definition: Count number of 1's and 0's of Integer with using bitwise operator.

2^32 = 4,294,967,296
unsigned int 32 bit
*/

#include<stdio.h>

int CountOnesFromIteger(unsigned int);

int main()
{
    unsigned int inputValue;
    short unsigned int onesOfValue;
    printf("Please Enter value (between 0 to 4,294,967,295) : ");
    scanf("%u",&inputValue);
    onesOfValue = CountOnesFromInteger(inputValue);

    printf("\nThe Number has \"%d\" 1's and \"%d\" 0's",onesOfValue,32-onesOfValue);
}

int CountOnesFromInteger(unsigned int value)
{
    unsigned short int i, count = 0;
    for(i = 0; i < 32 ; i++)
    {
        if (value % 2 != 0)
        {
            count++;
        }
        value = value >> 1;
    }
    return count;
}

Counting number of 1's and 0's from integer with bitwise operation

Is there a better way of doing this?

// Definition: Count number of 1's and 0's from integer with bitwise operation
//
// 2^32 = 4,294,967,296
// unsigned int 32 bit

#include<stdio.h>

int CountOnesFromInteger(unsigned int);

int main()
{
    unsigned int inputValue;
    short unsigned int onesOfValue;
    printf("Please Enter value (between 0 to 4,294,967,295) : ");
    scanf("%u",&inputValue);
    onesOfValue = CountOnesFromInteger(inputValue);

    printf("\nThe Number has \"%d\" 1's and \"%d\" 0's",onesOfValue,32-onesOfValue);
}

int CountOnesFromInteger(unsigned int value)
{
    unsigned short int i, count = 0;
    for(i = 0; i < 32 ; i++)
    {
        if (value % 2 != 0)
        {
            count++;
        }
        value = value >> 1;
    }
    return count;
}
Tweeted twitter.com/#!/StackCodeReview/status/416654357799915520
added 3 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Count Counting 1's and 0's of Integer by using bitwise operator. Do you find any other better way than this?

`/* Definition: Count number of 1's and 0'sIs there a better way of Integer with using bitwise operator. Coder : Jaysinh Shukla Email : [email protected]

2 power 32 : 4,294,967,296 unsigned int 32 bit. */doing this?

/*
Definition: Count number of 1's and 0's of Integer with using bitwise operator.

2^32 = 4,294,967,296
unsigned int 32 bit
*/

#include<stdio.h>

int CountOnesFromIteger(unsigned int);

int main()
{
    unsigned int inputValue;
    short unsigned int onesOfValue;
    printf("Please Enter value (between 0 to 4,294,967,295) : ");
    scanf("%u",&inputValue);
    onesOfValue = CountOnesFromInteger(inputValue);

    printf("\nThe Number has \"%d\" 1's and \"%d\" 0's",onesOfValue,32-onesOfValue);
}

int CountOnesFromInteger(unsigned int value)
{
    unsigned short int i, count = 0;
    for(i = 0; i < 32 ; i++)
    {
        if (value % 2 != 0)
        {
            count++;
        }
        value = value >> 1;
    }
    return count;
}`

Count 1's and 0's of Integer by using bitwise operator. Do you find any other better way than this?

`/* Definition: Count number of 1's and 0's of Integer with using bitwise operator. Coder : Jaysinh Shukla Email : [email protected]

2 power 32 : 4,294,967,296 unsigned int 32 bit. */

#include<stdio.h>

int CountOnesFromIteger(unsigned int);

int main()
{
    unsigned int inputValue;
    short unsigned int onesOfValue;
    printf("Please Enter value (between 0 to 4,294,967,295) : ");
    scanf("%u",&inputValue);
    onesOfValue = CountOnesFromInteger(inputValue);

    printf("\nThe Number has \"%d\" 1's and \"%d\" 0's",onesOfValue,32-onesOfValue);
}

int CountOnesFromInteger(unsigned int value)
{
    unsigned short int i, count = 0;
    for(i = 0; i < 32 ; i++)
    {
        if (value % 2 != 0)
        {
            count++;
        }
        value = value >> 1;
    }
    return count;
}`

Counting 1's and 0's of Integer by using bitwise operator

Is there a better way of doing this?

/*
Definition: Count number of 1's and 0's of Integer with using bitwise operator.

2^32 = 4,294,967,296
unsigned int 32 bit
*/

#include<stdio.h>

int CountOnesFromIteger(unsigned int);

int main()
{
    unsigned int inputValue;
    short unsigned int onesOfValue;
    printf("Please Enter value (between 0 to 4,294,967,295) : ");
    scanf("%u",&inputValue);
    onesOfValue = CountOnesFromInteger(inputValue);

    printf("\nThe Number has \"%d\" 1's and \"%d\" 0's",onesOfValue,32-onesOfValue);
}

int CountOnesFromInteger(unsigned int value)
{
    unsigned short int i, count = 0;
    for(i = 0; i < 32 ; i++)
    {
        if (value % 2 != 0)
        {
            count++;
        }
        value = value >> 1;
    }
    return count;
}
Source Link

Count 1's and 0's of Integer by using bitwise operator. Do you find any other better way than this?

`/* Definition: Count number of 1's and 0's of Integer with using bitwise operator. Coder : Jaysinh Shukla Email : [email protected]

2 power 32 : 4,294,967,296 unsigned int 32 bit. */

#include<stdio.h>

int CountOnesFromIteger(unsigned int);

int main()
{
    unsigned int inputValue;
    short unsigned int onesOfValue;
    printf("Please Enter value (between 0 to 4,294,967,295) : ");
    scanf("%u",&inputValue);
    onesOfValue = CountOnesFromInteger(inputValue);

    printf("\nThe Number has \"%d\" 1's and \"%d\" 0's",onesOfValue,32-onesOfValue);
}

int CountOnesFromInteger(unsigned int value)
{
    unsigned short int i, count = 0;
    for(i = 0; i < 32 ; i++)
    {
        if (value % 2 != 0)
        {
            count++;
        }
        value = value >> 1;
    }
    return count;
}`