counting 1's in a binary byte

This is a discussion on counting 1's in a binary byte within the C++ Programming forums, part of the General Programming Boards category; basically i need to know how to count the 1's in example: 10110001 then output : 4 (for this example) ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    33

    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;
    	
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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!).
    Last edited by twomers; 08-23-2006 at 11:32 AM.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    One way is to use the count() algorithm. http://www.cppreference.com/cppalgorithm/count.html
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    cout << endl << std::count(binary.begin(), binary.end(), '1') << endl;

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    thanks for the help guys.. ill see what i can come up with

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Hmm... I know how to do this in JavaScript. I'll try to translate.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    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

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    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. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This gets asked all the time. Try searching the forum.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by muran_pling
    basically i need to know how to count the 1's in example: 10110001
    Spoiler.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    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

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > for (int i=0;i<=8;i++)
    This runs off the end of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    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

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd suggest getting a better C++ book. Haven't heard of string literals yet I guess huh?
    Code:
    char number[] = "11110000";

    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21