Hi everbody,
I have a newbie question for you, I have c++ string in my hand, and i want to have the bit representation of each letter of this string, how can i do this?
Thanks
Hi everbody,
I have a newbie question for you, I have c++ string in my hand, and i want to have the bit representation of each letter of this string, how can i do this?
Thanks
Do you know how to do it for a single character? If so, then just do that for each character in the string.
If not . . .
You can use
to get the value of the rightmost bit. Then you can useCode:number & 1
to shift the bits to the right. But using that method will generate the bits in reverse order. If you know how many bits there are (ie, 8), then it might be better to useCode:number >>= 1
or something.Code:mask = UCHAR_MAX; n = 7; loop number & mask >> n; mask /= 2 n --
Also note that the MSB (rightmost digit) will probably be 1 for negative numbers, depending on your compiler's implementation.
Also see this (somewhat advanced) FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1044780608
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Unless this is an assignment intended to teach bit manipulation, you can just put the char into a bitset and then output the bitset.
Thank you very much dwks, then to get the LSB of the character, i can say
bool temp=char & 1;
Did i understand i right?
Exactly. (Except that you can't have a variable called char. )
Then you can use
to eliminate the LSB, shifting all of the bits to the right.Code:variable = variable >> 1;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
The bitset idea looks really good.
"If you tell the truth, you don't have to remember anything"
-Mark Twain