# counting 1's in a binary byte

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-23-2006
muran_pling
counting 1's in a binary byte
basically i need to know how to count the 1's in example: 10110001

then output : 4 (for this example)

here is my code.. im trying to figure out how to count the 1's in the binary bytes outputted. Im thinking i may have to add a if statement that if bit = 1, add a count ??? help please :) thanks

Code:

```#include <cstdlib> #include <iostream> using namespace std; void binary(int); int main() {     string name = "";     cout<< "Please enter your name: ";     cin >> name;   for ( int i=0; i<(int)name.size(); i++ ) {   cout<< '\n' ;   binary((int)name[i]);   cout <<" "; }     cout<<endl;     system("PAUSE");     return EXIT_SUCCESS; } void binary(int number) {         int remainder;         if(number <= 1)     {                 cout << number;                 return;         }         remainder = number%2;         binary(number >> 1);            cout << remainder;         }```
• 08-23-2006
Salem
> Im thinking i may have to add a if statement that if bit = 1, add a count ???
A spendid idea - try it and find out.
• 08-23-2006
twomers
I would strongly looking into the <bitset> header. It's great! You can do things like

int i = 0;
bitset<8> thing;

and I can't remember how (haven't used it in a long time), but you can assign the value of a variable to the 'thing' variable. And there's a counting function to could the number of 1's etc. the <8> specifies it's eight bts (EDIT :: Had bytes written there, whoops. It's 8 BITS!).
• 08-23-2006
Mario F.
One way is to use the count() algorithm. http://www.cppreference.com/cppalgorithm/count.html
• 08-23-2006
indigo0086
cout << endl << std::count(binary.begin(), binary.end(), '1') << endl;
• 08-23-2006
muran_pling
thanks for the help guys.. ill see what i can come up with
• 08-23-2006
mburt
Hmm... I know how to do this in JavaScript. I'll try to translate.
• 08-23-2006
muran_pling
Quote:

Originally Posted by mburt
Hmm... I know how to do this in JavaScript. I'll try to translate.

thanks u. ill wait for ur responce
• 08-23-2006
mburt
Got it. The only downside is that you have to store the binary number in an array.

See this:

Code:

```#include <iostream> using namespace std; int main() { int number[] = {1,1,1,1,0,0,0,0}; int count = 0; for (int i=0;i<=8;i++) { if (number[i]==1) {count++;}; }; cout << count; system("PAUSE"); }```
• 08-23-2006
quzah
This gets asked all the time. Try searching the forum.

Quzah.
• 08-23-2006
Dave_Sinkula
Quote:

Originally Posted by muran_pling
basically i need to know how to count the 1's in example: 10110001

Spoiler.
• 08-23-2006
muran_pling
Quote:

Originally Posted by mburt
Got it. The only downside is that you have to store the binary number in an array.

See this:

Code:

```#include <iostream> using namespace std; int main() { int number[] = {1,1,1,1,0,0,0,0}; int count = 0; for (int i=0;i<=8;i++) { if (number[i]==1) {count++;}; }; cout << count; system("PAUSE"); }```

thanks for that. i will try work with it!!! much appreciated
• 08-24-2006
Salem
> for (int i=0;i<=8;i++)
This runs off the end of the array.
• 08-24-2006
muran_pling
Quote:

Originally Posted by mburt
Got it. The only downside is that you have to store the binary number in an array.

See this:

Code:

```#include <iostream> using namespace std; int main() { int number[] = {1,1,1,1,0,0,0,0}; int count = 0; for (int i=0;i<=8;i++) { if (number[i]==1) {count++;}; }; cout << count; system("PAUSE"); }```

is there a way to get rid of the commers in the array so for example... 1,1,1,1,0,0,0,0 is inputed as 11110000 and still reads 4 1s ? i tryed it without commers and it reads there is only 1 1's in the byte
• 08-24-2006
quzah
I'd suggest getting a better C++ book. Haven't heard of string literals yet I guess huh?
Code:

`char number[] = "11110000";`

Quzah.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last