By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

bitset woes

P: n/a
I have tried reading all the docs and I suppose I am just not parsing
them correctly so I'll ask here

suppose I have three bitsets as follows:
bitset<48> a;
bitset<48> b;
bitset<48> c:

I want to bit-by-bit and the contents of a and b and have them placed
in c, what does the syntax look like for this. I thought, given that
some of the operators are overloads, that i might look like

c = a &b

this does not work however
Thanks in advance

Jun 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
It works for me. I'm using g++

g++ (GCC) 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)

to compile this

<code>
#include <bitset>
#include <iostream>

using namespace std;

int main()
{
bitset<48> a, b, c;

for (int i = 0; i < 48; ++i)
{
b[i] = rand() % 2;
a[i] = rand() % 2;
}
c = a & b;
cout << a << endl << b << endl << c << endl;
}
</code>

What compiler are you using? What exactly doesn't work?

Paul Richardson wrote:
I have tried reading all the docs and I suppose I am just not parsing
them correctly so I'll ask here

suppose I have three bitsets as follows:
bitset<48> a;
bitset<48> b;
bitset<48> c:

I want to bit-by-bit and the contents of a and b and have them placed
in c, what does the syntax look like for this. I thought, given that
some of the operators are overloads, that i might look like

c = a &b

this does not work however
Thanks in advance


Jun 18 '06 #2

P: n/a
Care to take on the usage of to_string()
On 2006-06-18 06:14:23 -0700, st************@gmail.com said:
It works for me. I'm using g++

g++ (GCC) 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)

to compile this

<code>
#include <bitset>
#include <iostream>

using namespace std;

int main()
{
bitset<48> a, b, c;

for (int i = 0; i < 48; ++i)
{
b[i] = rand() % 2;
a[i] = rand() % 2;
}
c = a & b;
cout << a << endl << b << endl << c << endl;
}
</code>

What compiler are you using? What exactly doesn't work?

Paul Richardson wrote:
I have tried reading all the docs and I suppose I am just not parsing
them correctly so I'll ask here

suppose I have three bitsets as follows:
bitset<48> a;
bitset<48> b;
bitset<48> c:

I want to bit-by-bit and the contents of a and b and have them placed
in c, what does the syntax look like for this. I thought, given that
some of the operators are overloads, that i might look like

c = a &b

this does not work however
Thanks in advance

Jun 18 '06 #3

P: 4
I have not do C++ from sometime now, but I am back to the C++/C world for a new job.

Tried the above code, made a few modifications, everything seems fine, except the exclusive or get all zeros, don't know why.

Used gcc base compiler dev-c++.

Here is my code, please kindly find my possible errors. Thanks!
Expand|Select|Wrap|Line Numbers
  1. #include <bitset>
  2. #include <iostream>
  3. using namespace std;
  4. #define SET_SIZE 32
  5. int main() {
  6.   bitset<SET_SIZE> a, b;
  7.   bitset<SET_SIZE> c, d, e;
  8.   for (int i=0; i<SET_SIZE; ++i) {
  9.     b[i] = rand() % 2;
  10.     a[i] = rand() % 2;
  11.   }
  12.   c = a & b;
  13.   d = a | b;
  14.   d = a ^ b;
  15.   cout << a << endl << b << endl;
  16.   cout << c << endl << d << endl << e << endl;
  17.   cout << (3 ^ 4) << endl;
  18.   system("pause");
  19. }
  20.  
a ranchgirl
Jun 18 '06 #4

P: 4
Sorry, ignore my post! I found I had a typo... :(
Jun 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.