-2

I was looking for some STL support for binary strings. bitset appears to be very useful, however I couldn't manipulate the individual bits sucessfully.

#include <iostream>
#include <bitset>

using namespace std;

int main()
{
    string b = bitset<8>(128).to_string();

    for(auto &x:b)
    {
        x = 1 and x-'0' ; cout<<b<<"\n";
    }

    return 0;
}

So, should I use vector or bitset can be used for manipulating individual bits ?

The above program gives:

☺0000000
☺ 000000
☺  00000
☺   0000
☺    000
☺     00
☺      0
☺

I know this happens because I am manipulating char, which when set to 0 prints the associated ascii character. My question is can I loop through a bitset and simultaneously modify the individual bits?

For example I surely can't do below :

#include <iostream>       
#include <string>         
#include <bitset>        
int main ()
{
  std::bitset<16> baz (std::string("0101111001"));
  std::cout << "baz: " << baz << '\n';

  for(auto &x: baz)

  {
      x = 1&x;

  }

std::cout << "baz: " << baz << '\n';

  return 0;
}
adrian008
  • 395
  • 6
  • 18
  • by `and` do you want bitwise and? if yes then you should use `&` – Saeid Jun 26 '15 at 21:02
  • Bitset is going to be easiest for manipulating individual bits. You can easilly access each bit using the [] operator. – ChrisD Jun 26 '15 at 21:02
  • It may be worth mentioning that `1 & x` is equivalent to the identity function, because `(1 & x) == x`, so this code wouldn't modify the values even if it were correct. – Apples Jun 26 '15 at 21:05
  • @AntonSavin I know, plz see the edit. – adrian008 Jun 26 '15 at 21:07
  • @AlchemicalApples That doesn't matter for the question. plz see the edit. – adrian008 Jun 26 '15 at 21:08
  • @adrian008 Well, regardless, you can't use the `for (auto& x : b) { ... }` syntax with `bitset`, but `for (auto i=0u; i – Apples Jun 26 '15 at 21:11
  • @AlchemicalApples Also, I can't initialize a bitset with a variable number ? – adrian008 Jun 26 '15 at 21:15
  • @AlchemicalApples I cant do this :::::::#include #include using namespace std; int main() { unsigned long int s; cin>>s; string binary = bitset<8>(s).to_string(); cout<(binary).to_ulong(); cout< – adrian008 Jun 26 '15 at 21:16
  • @adrian008 That code works for me. Also, there's no need to use `.to_string()` all the time. You can use `cout<<` on a `bitset` directly. – Apples Jun 26 '15 at 21:20
  • @AlchemicalApples The above code in comment ? It works ?? – adrian008 Jun 26 '15 at 21:23
  • @adrian008 [http://ideone.com/0PdY3B](http://ideone.com/0PdY3B) – Apples Jun 26 '15 at 22:03

1 Answers1

6

You can easily manipulate bits of std::bitset using set,reset,flip, operator[] methods. See http://www.cplusplus.com/reference/bitset/bitset/

// bitset::reset
#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo (std::string("1011"));

  std::cout << foo.reset(1) << '\n';    // 1001
  std::cout << foo.reset() << '\n';     // 0000

  return 0;
}
Ashot
  • 10,807
  • 14
  • 66
  • 117