473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

bitset woes

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
4 1911
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
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
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
Sorry, ignore my post! I found I had a typo... :(
Jun 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hunter Hou | last post by:
Hello, I have this very simple program, but it can't be compiled. What's wrong here? thanks, hunter #include <iostream>
2
by: shaun roe | last post by:
As a follow up to my question about STL and bitset<64> I'd like to share a quirk (bug?) about unsigned long long support and the bitset. I'm using gcc 3.2 on linux or gcc 3.3 on mac, the answer is...
5
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): ...
5
by: Rich S. | last post by:
Hi, Is the code below the best way to have the less than function for an 80 bit bitset or is there something faster / better? When I populate this map with millions (... and millions) of sets...
14
by: Haro Panosyan | last post by:
How to construct a bitset from string? For example: std::bitset<16> b16("1011011110001011"); Is this possible? Thanks in advance. -haro
3
by: shaun | last post by:
I have a function for returning the value of a bit field in a number: template<typename T> T bitfield(const T num, const unsigned int bitStart, const unsigned int bitEnd){ T mask,...
4
by: Sarath | last post by:
>From the documentation of MSDN, it is saying that bitset is not a STL container Unlike the similar vector<boolClass, the bitset class does not have iterators and is not an Standard Template...
2
by: arnuld | last post by:
i am confused on some aspects of bitset class: /* C++ Primer 4/e * chapter 3 * * exercise 3.23 * */ #include <iostream>
5
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; //...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.