Thread: counting 1's in a binary byte

  1. #16
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    Quote Originally Posted by quzah
    I'd suggest getting a better C++ book. Haven't heard of string literals yet I guess huh?
    Code:
    char number[] = "11110000";

    Quzah.

    Code:
    char number[] = "11110000";
    ??
    was that ment to replace int number[] = {1,1,1,1,0,0,0,0}; array ?

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't tell me you don't know how to do some basic subtraction.


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

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No need for subtraction with such a null terminated string, but from the initial post it looks like the input is supposed to be some integer, not a string containing a representation of an integer in binary.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Which is why we use subtraction...

    Hint: - '0'


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

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hint: - '0'
    That is going to be rather redundant, in my opinion (as I stated in my previous post). One can simply count the '1' chars directly from the null terminated string. On the other hand, if you are provided with an integer, such subtraction will not work, since we cannot subtract a char from a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    	remainder = number%2; // == "remainder = number & 1"
    Faster and more elegant.

    Code:
    void binary(int number) 
    {
    	//... code ...
    	binary(number >> 1);
    }
    Why? Use a loop. While you're at it put counting code into your loop and you would have solved your problem.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #22
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by mburt
    Got it. The only downside is that you have to store the binary number in an array.
    So, you don't got it? Dissect this bit of probably non-portable code:
    Code:
    #include <iostream>
    #include <string>
    
    long binaryCount(long number)
    {
    	number = (number & 0x55555555) + ((number & 0xaaaaaaaa) >> 1);
    	number = (number & 0x33333333) + ((number & 0xcccccccc) >> 2);
    	number = (number & 0x0f0f0f0f) + ((number & 0xf0f0f0f0) >> 4);
    	number = (number & 0x00ff00ff) + ((number & 0xff00ff00) >> 8);
    	return   (number & 0x0000ffff) + ((number & 0xffff0000) >> 16);
    }
    
    int main()
    {
    	std::string input = "Awesome";
    
    	long finalCount = 0;
    	for(int i = 0; input[i]; i++)
    		finalCount += binaryCount(input[i]);
    
    	std::cout << finalCount << std::endl;
    
    	return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight
    That is going to be rather redundant, in my opinion (as I stated in my previous post). One can simply count the '1' chars directly from the null terminated string. On the other hand, if you are provided with an integer, such subtraction will not work, since we cannot subtract a char from a string.
    Sure it's redundant. He didn't ask for the best solution, Dave's already posted a link to those. He asked how to initialize it without putting , between them. Asked. Answered.


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

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >im trying to figure out how to count the 1's in the binary bytes outputted.
    Code:
    #include <bitset>
    .
    .
       for ( int i=0; i<(int)name.size(); i++ )
       {
          cout << "1's in " << name[i] << ": " << std::bitset<8>(name[i]).count() << endl;
       }

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, 05:49 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03: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, 03: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