Thread: SUM character to Binary

1. 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

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. 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)`

3. 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. 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. 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++;
}
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. Nice.

7. 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. 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. 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```

10. 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.

Popular pages Recent additions