About very long Bit-numbers (bitfields?)

J

Juha Kettunen

Hi

I don't know if I am using right words (bit-number), but this is what I
mean:

You can set a 64 bit number:

unsigned long a;

Now you can use binary operators to manipulate variable a (for example
a |= (unsigned long) 128
).

How about if I want to get for example a 640 bit long *one* number (say b)
and do the same binary operations to it (b |= 128)? I haven't found answer
for this ... do I need to use Bitfields:

struct BITS
{
unsigned long b1;
unsigned long b2;
unsigned long b3;
.... (10 times)
};

I don't know ...
 
J

Juha Kettunen

ok , thanks for you and Christopher. I will check that vector<bool>.

Hmm but I think cannot do bit operations for it:

vector<bool> a;

a |= 128;

The main point is, that I really need to be able to do similar operations
than bit operations to get some benefit in my code (That means, that set
*many* bits at the same time, as b |= 128 does). But maybe I can do it with
that vector ....

But anyway, it seems to be second best (better than normal arrays) if I
cannot use bit operations...

Leor Zolman said:
Hi

I don't know if I am using right words (bit-number), but this is what I
mean:

You can set a 64 bit number:

unsigned long a;

Just FYI, the number of bits in a long (unsigned or otherwise) is
platform-dependent. On my system they're only 32 bits. But that doesn't
have much bearing on your real question, which is how to get /real/ long
bit sequences.
How about if I want to get for example a 640 bit long *one* number (say b)
and do the same binary operations to it (b |= 128)? I haven't found answer
for this ... do I need to use Bitfields:

For that, Christopher's suggestion of vector<bool> may be adequate.
vector<bool> is the "black sheep" of the STL container family...because it
doesn't meet all the usual vector requirements:

#include <vector>

int main()
{
std::vector<char> vc;
std::vector<bool> vb;

vc.push_back('x');
vc.push_back('y');
vb.push_back(true);
vb.push_back(true);

std::vector<char>::iterator vcit = vc.begin();
std::vector<bool>::iterator vbit = vb.begin();

*vcit = 'z'; // OK
*vbit = false; // OK

char *cp = &vc[0]; // OK
bool *bp = &vb[0]; // Oops: won't compile.

return 0;
}

On the other hand, it provides some operation specialized for bits that
std::vector<something-other-than-bool> does not. Josuttis 6.2.6 has the
gory details. If you can avoid the potholes, it may work fine for you.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
J

Juha Kettunen

Christopher Benson-Manica said:
You might look at vector<bool>, although I have no idea how useful
you'll find it.

Actually, when I was checking from the "Stroustrup C++" this vector issue
just now, I recognized, that there is a std::bitset vector type as well,
which is definetely most suitable for me: It handles binary values and have
all of those bit operations |=, &=, and so on.

But thanks, i didnt really even think that vectors could solve my problem
.... (I thought i must be a struct).

I will try it now, and see if it *really* works :)...
 
J

Juha Kettunen

Leor Zolman said:
Yes, that's clearly a better starting point, and I'm kicking myself for
having forgotten about std::bitset. So as not to depart this thread without
having at least /something/ helpful to say, let me recommend that you get
yourself a copy of Josuttis' "The C++ Standard Library". It is
indispensable when trying to choose and then use STL facilities... and it
has ten pages on bitset.
-leor

Now I tried that bitset, and ... hmmm.. there is one "problem": it is too
slow! I need to have very fast calculations, and normal bit operations with
integers does that, but it seems to me that bitset is slow. I did two tests:

1.
bitset<31> a;

for (int i=1;i<40000000;i++)
{
a |= (11 << 10);
}


2.
unsigned long f;

for (int i=1;i<40000000;i++)
{
f |= (11 << 10);
}

The first one took about 10 seconds to complete, but the second one
basically 0 seconds!

bitset does the job, but is too slow.

Is there a way to do it fast (basically I mean that if you use bitset<300>
a;)?
 
J

Juha Kettunen

Leor Zolman said:
First thing I tested. I believe the performance gap is real. YMMV...
-leor

I tested it as well with variable (runs from 1 to 11) , and again about 0
seconds.
 
C

Christopher Benson-Manica

Juha Kettunen said:
How about if I want to get for example a 640 bit long *one* number (say b)
and do the same binary operations to it (b |= 128)? I haven't found answer
for this ... do I need to use Bitfields:

You might look at vector<bool>, although I have no idea how useful
you'll find it.
 
L

Leor Zolman

Hi

I don't know if I am using right words (bit-number), but this is what I
mean:

You can set a 64 bit number:

unsigned long a;

Just FYI, the number of bits in a long (unsigned or otherwise) is
platform-dependent. On my system they're only 32 bits. But that doesn't
have much bearing on your real question, which is how to get /real/ long
bit sequences.
How about if I want to get for example a 640 bit long *one* number (say b)
and do the same binary operations to it (b |= 128)? I haven't found answer
for this ... do I need to use Bitfields:

For that, Christopher's suggestion of vector<bool> may be adequate.
vector<bool> is the "black sheep" of the STL container family...because it
doesn't meet all the usual vector requirements:

#include <vector>

int main()
{
std::vector<char> vc;
std::vector<bool> vb;

vc.push_back('x');
vc.push_back('y');
vb.push_back(true);
vb.push_back(true);

std::vector<char>::iterator vcit = vc.begin();
std::vector<bool>::iterator vbit = vb.begin();

*vcit = 'z'; // OK
*vbit = false; // OK

char *cp = &vc[0]; // OK
bool *bp = &vb[0]; // Oops: won't compile.

return 0;
}

On the other hand, it provides some operation specialized for bits that
std::vector<something-other-than-bool> does not. Josuttis 6.2.6 has the
gory details. If you can avoid the potholes, it may work fine for you.
-leor
 
L

Leor Zolman

Actually, when I was checking from the "Stroustrup C++" this vector issue
just now, I recognized, that there is a std::bitset vector type as well,
which is definetely most suitable for me: It handles binary values and have
all of those bit operations |=, &=, and so on.

Yes, that's clearly a better starting point, and I'm kicking myself for
having forgotten about std::bitset. So as not to depart this thread without
having at least /something/ helpful to say, let me recommend that you get
yourself a copy of Josuttis' "The C++ Standard Library". It is
indispensable when trying to choose and then use STL facilities... and it
has ten pages on bitset.
-leor
 
J

Julie

Juha said:
Now I tried that bitset, and ... hmmm.. there is one "problem": it is too
slow! I need to have very fast calculations, and normal bit operations with
integers does that, but it seems to me that bitset is slow. I did two tests:

1.
bitset<31> a;

for (int i=1;i<40000000;i++)
{
a |= (11 << 10);
}

2.
unsigned long f;

for (int i=1;i<40000000;i++)
{
f |= (11 << 10);
}

The first one took about 10 seconds to complete, but the second one
basically 0 seconds!

bitset does the job, but is too slow.

Is there a way to do it fast (basically I mean that if you use bitset<300>
a;)?

Careful there -- did you examine the executable code for loop 2? As far as I
know, it isn't unreasonable to expect an optimizing compiler to reduce that to
a single statement since the loop operand is constant.

Recreate your test using an operand that isn't constant, verify the executable
code, and re-run the test.
 
L

Leor Zolman

Careful there -- did you examine the executable code for loop 2? As far as I
know, it isn't unreasonable to expect an optimizing compiler to reduce that to
a single statement since the loop operand is constant.

Recreate your test using an operand that isn't constant, verify the executable
code, and re-run the test.

First thing I tested. I believe the performance gap is real. YMMV...
-leor
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top