Reputation: 7491
I was wondering what is the best way to interpret "bit string"?
for example:
a bit string like "1010010" is feed to the following function
void foo (string s1) {
// some code to do bit manipulation of the bit string
}
What is the best way to do it? Many thanks!!!
Upvotes: 1
Views: 126
Reputation: 361812
If you just want to convert string into its integral value, then std::stoi
family could help:
int value = std::stoi("10100"); //value will be 10100, not 20
If you want to manipulate bit patterns represented by string, then std::bitset
might help you in some way:
std::bitset<32> bitpattern("10100");
//you can manupulates bitpattern as you wish
//see the member functions of std::bitset
//you can also convert into unsigned long
unsigned long ul = bitpattern.to_ulong(); //ul will be 20, not 10100
Upvotes: 2