ach
ach

Reputation: 195

Bit operations on an arbitrarily large bit-array or number

I have a very simple question: In C++, is there an in-built or straightforward way to group a large (~1000) number of bits (or bools) in a single label such that the inbuilt bit operators function as they do for fundamentals?

e.g. for a long you might write:

unsigned long maximum = ~0;

or one might use:

somenum>>;

Is there an analogous way to do this for a block of memory of arbitrary size?

If not, what are some good alternatives ? I have thought of bit <vectors>, a C union, etc., but these all seem to require handwritten routines for the various bit operations.

Upvotes: 6

Views: 773

Answers (2)

user283145
user283145

Reputation:

Also, boost::dynamic_bitset might be useful depending on the requirements. I wish it was standard instead of the hack that std::vector<bool> is

Upvotes: 1

templatetypedef
templatetypedef

Reputation: 372814

Yep! It's called std::bitset and does just that.

Hope this helps!

Upvotes: 11

Related Questions