# Thread: counting 1's in a binary byte

1. ## 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 = "";

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;

}``` 2. > 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. 3. 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!). 4. One way is to use the count() algorithm. http://www.cppreference.com/cppalgorithm/count.html 5. cout << endl << std::count(binary.begin(), binary.end(), '1') << endl; 6. thanks for the help guys.. ill see what i can come up with 7. Hmm... I know how to do this in JavaScript. I'll try to translate. 8. 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 9. 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");
}``` 10. This gets asked all the time. Try searching the forum.

Quzah. 11. Originally Posted by muran_pling
basically i need to know how to count the 1's in example: 10110001
Spoiler. 12. 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 13. > for (int i=0;i<=8;i++)
This runs off the end of the array. 14. 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 15. I'd suggest getting a better C++ book. Haven't heard of string literals yet I guess huh?
Code:
`char number[] = "11110000";`

Quzah. Popular pages Recent additions 