Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manage bits/binary in c++?

What I need to do is open a text file with 0s and 1s to find patterns between the columns in the file.

So my first thought was to parse each column into a big array of bools, and then do the logic between the columns (now in arrays). Until I found that the size of bools is actually a byte not a bit, so i would be wasting 1/8 of memory, assigning each value to a bool.

Is it even relevant in a grid of 800x800 values? What would be the best way to handle this? I would appreciate a code snippet in case its a complicated answer

like image 649
Agush Avatar asked Dec 13 '22 18:12

Agush


1 Answers

You could use std::bitset or Boosts dynamic_bitset which provide different methods which will help you manage your bits.

They for example support constructors which create bitsets from other default types like int or char. You can also export the bitset into an ulong or into a string (which then could be turned into a bitset again etc)

I once asked about concatenating those, which wasn't performantly possible to do. But perhaps you could use the info in that question too.

like image 60
MOnsDaR Avatar answered Dec 27 '22 19:12

MOnsDaR