473,320 Members | 2,027 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,320 software developers and data experts.

An array of bools

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

bool BArray[100];

vector<bool> BArray;

valarray<bool> BArray;

Thanks in advance!
Jul 22 '05 #1
5 2913
ccs wrote:
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
Jul 22 '05 #2
ccs
Pete C. <x@x.x> wrote in message
news:%J******************@newsread2.news.pas.earth link.net...
ccs wrote:
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?
Jul 22 '05 #3
ccs wrote:
Pete C. <x@x.x> wrote in message
news:%J******************@newsread2.news.pas.earth link.net...
ccs wrote:
> 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
Jul 22 '05 #4
> Pete C. <x@x.x> wrote in message
news:%J******************@newsread2.news.pas.earth link.net...
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;
}
Jul 22 '05 #5
"AngleWyrm" <no***************@hotmail.com> wrote in message
news:irPyc.90651$Ly.18169@attbi_s01...
Pete C. <x@x.x> wrote in message
news:%J******************@newsread2.news.pas.earth link.net...

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;
}


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";
}
Jul 22 '05 #6

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

Similar topics

32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
9
by: Davids | last post by:
is it true that I cannot dynamically add an item to an array? Eg public char = {"a","b"}; char.Add("newitem"); Do I really have to switch to ArrayList to do this?
4
by: gpg | last post by:
I am using a legacy DLL and need to marshal some structures for use in the DLL. For the most part, I have figured out my needs except for one small item. I have a structure that contain, among...
8
by: james | last post by:
Hi, Just looking here: http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx I can't quite see what I want to do. I want an array of Booleans of length 102, all initially "false". I have this...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
5
by: Randeh | last post by:
Short story: in a beginning C++ class in college, was in a car accident that caused central spinal stenosis, some new Schmorl's nodes, disc problems and an incredible amount of pain. So far I've...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.