patterncMajor
Counting number of 1's and 0's from integer with bitwise operation
Viewed 0 times
numbercountingwithoperationbitwiseandfrominteger
Problem
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
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 > 1;
}
return count;
}Solution
Yes, there is a better way:
The code relies on the fact that the expression
This approach is superior because it doesn't depend on an integer's size - it's totally portable - and it tests every bit in a fairly efficient way (with the comparison
Also, you might want to replace
int CountOnesFromInteger(unsigned int value) {
int count;
for (count = 0; value != 0; count++, value &= value-1);
return count;
}The code relies on the fact that the expression
x &= x-1; removes the rightmost bit from x that is set. We keep doing so until no more 1's are removed. This technique is described in K&R.This approach is superior because it doesn't depend on an integer's size - it's totally portable - and it tests every bit in a fairly efficient way (with the comparison
value != 0).Also, you might want to replace
32 in main() with sizeof(int)*CHAR_BIT so that your code doesn't depend on an integer using 32 bits:#include
#include
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 \"%zu\" 0's",onesOfValue,sizeof(int)*CHAR_BIT-onesOfValue);
return 0;
}Code Snippets
int CountOnesFromInteger(unsigned int value) {
int count;
for (count = 0; value != 0; count++, value &= value-1);
return count;
}#include <stdio.h>
#include <limits.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 \"%zu\" 0's",onesOfValue,sizeof(int)*CHAR_BIT-onesOfValue);
return 0;
}Context
StackExchange Code Review Q#38182, answer score: 28
Revisions (0)
No revisions yet.