An array of bools

C

ccs

Which way should chose, based on tradeoff, performance etc.?

bool BArray[100];

vector<bool> BArray;

valarray<bool> BArray;

Thanks in advance!
 
P

Pete C.

ccs said:
Which way should chose, based on tradeoff, performance etc.?

bool BArray[100];

vector<bool> BArray;

valarray<bool> BArray;

Thanks in advance!

What do you want to do with it? There's std::bitset<100> also.

- Pete
 
C

ccs

Pete C. said:
ccs said:
Which way should chose, based on tradeoff, performance etc.?

bool BArray[100];

vector<bool> BArray;

valarray<bool> BArray;

Thanks in advance!

What do you want to do with it? There's std::bitset<100> also.

- Pete
Thanks. It's for recording a group of switches (on or off).
If using std::bitset<100>, should "100" be the number of bits?
Could you give an example to set 5th bit to 1? How to check if 7th bit is 1
or 0?
 
K

Kai-Uwe Bux

ccs said:
Pete C. said:
ccs said:
Which way should chose, based on tradeoff, performance etc.?

bool BArray[100];

vector<bool> BArray;

valarray<bool> BArray;

Thanks in advance!

What do you want to do with it? There's std::bitset<100> also.

- Pete
Thanks. It's for recording a group of switches (on or off).

You asked for performance and tradeoffs. In order to anwser that question,
the *meaning* of the bits is not as important as *which algorithms* you
will typically want to apply to the array/vector/valarray/bitset. Will you
need to copy it often, will you need to iterate through it often, do you
need to count the number of non-zero bits, etc.?

However, keep in mind that performance is something to worry about at the
very end. I would suggest to write your programm in a very clean way so
that you can change this particular data structure without too much effort.
Since performance also depends on the compiler and library, you will want
to do some measurements anyway.
If using std::bitset<100>, should "100" be the number of bits?
Could you give an example to set 5th bit to 1? How to check if 7th bit is
1 or 0?

std::bitset<100> bits;

bits[5] = 1;

if ( bits.test(7) ) {
// 1;
} else {
// 0
}


There are many other methods e.g., for flipping bits and counting the
number of bits set.


Best

Kai-Uwe
 
A

AngleWyrm

Pete C. said:
Thanks. It's for recording a group of switches (on or off).
If using std::bitset<100>, should "100" be the number of bits?
Could you give an example to set 5th bit to 1? How to check if 7th bit is 1
or 0?


#include <iostream>
#include <bitset>
using namespace std;

int main() {

bitset<8> switches(0xAF); // 1010 1111
const bitset<8> MASK(0xF5); // 1111 0101

cout << "switches: " << switches << endl
<< " mask: " << MASK << endl << endl

<< " AND: " << (switches & MASK)
<< " -- turn off switches masked as 0\n"

<< " OR: " << (switches | MASK)
<< " -- turn on switches masked as 1\n"

<< " XOR: " << (switches ^ MASK)
<< " -- flip switches masked as 1"
<< endl;
}
 
A

AngleWyrm

AngleWyrm said:
#include <iostream>
#include <bitset>
using namespace std;

int main() {

bitset<8> switches(0xAF); // 1010 1111
const bitset<8> MASK(0xF5); // 1111 0101

cout << "switches: " << switches << endl
<< " mask: " << MASK << endl << endl

<< " AND: " << (switches & MASK)
<< " -- turn off switches masked as 0\n"

<< " OR: " << (switches | MASK)
<< " -- turn on switches masked as 1\n"

<< " XOR: " << (switches ^ MASK)
<< " -- flip switches masked as 1"
<< endl;
}

You can also inspect or set individual bitset elements using the array
operator[], like so:
switches[5] = true;
if( switches[7] )
{
cout<< "switch seven is on";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top