Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 200
ach Avatar asked Aug 27 '12 20:08

ach


People also ask

When would you use bit operations?

Because they allow greater precision and require fewer resources, bitwise operators can make some code faster and more efficient. Examples of uses of bitwise operations include encryption, compression, graphics, communications over ports/sockets, embedded systems programming and finite state machines.

What are bit level operations?

Bit shift operators These operators shift bit sequences to the left or right by a number of spaces, padding with 0 bits and dropping any bits that fall off the number. Here are some examples. unsigned char x = 0xD5; // 11100101 x = x << 2; // x is now 10010100 x = x >> 3; // x is now 00010010.

Which logic operation is used for setting a bit without modifying other bits?

For example, this is necessary to change settings in the microcontroller or other devices. We use bitwise operators to change certain bits while not affecting others.


1 Answers

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

Hope this helps!

like image 148
templatetypedef Avatar answered Nov 15 '22 11:11

templatetypedef