SUM character to Binary

This is a discussion on SUM character to Binary within the C++ Programming forums, part of the General Programming Boards category; i am definitely a newbie here, i being thinking of this code for 2 hours and that kill my brain.. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    SUM character to Binary

    i am definitely a newbie here, i being thinking of this code for 2 hours and that kill my brain.. so i reach here to ask for help.

    my problem is i couldnt sum up the binary number , for example

    character 'd' = 1100100 and 'e' = 1100101
    the answer is add up the one which is d = 3 + e = 4 sum = 7
    so the sum (cout<<sum will be 7

    and can some one explain this code wich i got it in this forum

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <bitset>
    using namespace std;
    
    int main()
    {
        string name;
        string lowerCase;
        int sum = 0;
        int total = 0;
        //prompt user input
        
        cout<<"Please enter your name to generate password: ";
        cin>>name;
    
        for( string::iterator it = name.begin(); it != name.end(); ++it )
            cout << bitset<7>((unsigned long)*it) <<" ";
    
        system("pause");
        return 0;
    }
    anyone can give another example similar code that is easy to understand?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I can't make any sense of this:
    my problem is i couldnt sum up the binary number , for example

    character 'd' = 1100100 and 'e' = 1100101
    the answer is add up the one which is d = 3 + e = 4 sum = 7
    so the sum (cout<<sum will be 7
    This:
    Code:
    for( string::iterator it = name.begin(); it != name.end(); ++it )
            cout << bitset<7>((unsigned long)*it) <<" ";
    displays the binary representation of every character in a string variable called 'name'. An iterator is like a pointer, and name.begin() is a pointer to the first char in name, and name.end() is a pointer to one past the last char in name. *it is the character. This part:
    Code:
    (unsigned long)*it
    converts the char to it's ascii code. It would more properly be written as:
    Code:
    static_cast<unsigned long>(*it)
    Last edited by 7stud; 12-03-2005 at 08:22 PM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    ok for example
    character 'd' = 1100100 AND // 3's 1
    character 'e' = 1100101 ADD TOGETHER // 4's 1
    TOTAL = 7 // 3 + 4 = 7

    not adding by using binary method and just sum all one together..

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) You have to convert the char to its ascii code
    2) Then you need to obtain a binary representation of the ascii code
    3) Then you can check each char of the binary representation and see if it is a '1'. If it is, then add 1 to a counter variable.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,868
    Your question is a little bit hard to understand and the code you are showing does not have much to do with what I understand.
    Could this be what you are looking for ?
    Code:
    #include <iostream>
    
    int num_set_bits( unsigned char c ) {
       int ret = 0;
       unsigned char mask = 0x01;
       int i;
       for ( i = 0; i < 8; ++i ) {
          if ( c & mask ) ret++;
          mask <<= 1;      
       }
       return ret;
    }
    
    int main() {
       unsigned char e = 'e';
       unsigned char d = 'd';
       std::cout << num_set_bits(e) + num_set_bits(d) << std::endl;
    }
    Kurt

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Nice.
    Last edited by 7stud; 12-04-2005 at 05:20 AM.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    Question

    yeah tat is the solution that i want but what if the input is a string of name = John

    the binary for John is 1001010 1101111 1101000 1101110
    TO ADD THOSE 1s = 3 + 6 + 3 + 5
    EQUAL = 17




  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,868
    Code:
       string name = "John";
       int sum = 0;
       for ( int i = 0; i < name.length(); i++ )
          sum += num_set_bits(name[i]);
       std::cout << sum << std::endl;
    Kurt

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,673
    bitsets also offer a count member function that returns the number of bits set to 1 which could be used to accomplish this:

    Code:
    string str = "John";
    int sum = 0;
    
    for( string::const_iterator it = str.begin(); it != str.end(); ++it )
    {
        bitset<7> bits((unsigned long)*it);
        cout << *it << " = " << bits << endl;
        sum += bits.count();
    }
    
    cout << "Sum is: " << sum << endl;
    Output:
    Code:
    J = 1001010
    o = 1101111
    h = 1101000
    n = 1101110
    Sum is: 17
    I used to be an adventurer like you... then I took an arrow to the knee.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The STL master--impressive.

    Unfortunately, it looks like gtr_s15 hasn't written a single line of code in his last 4 or 5 posts--all related to this program.
    Last edited by 7stud; 12-04-2005 at 10:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Repeatition
    By lgcarter in forum C Programming
    Replies: 3
    Last Post: 06-16-2009, 02:07 PM
  2. Replies: 1
    Last Post: 05-28-2009, 01:28 PM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. 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
  5. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 06:23 PM

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