0

Suppose I have a method that receives a buffer of fixed lenght, like this:

#define SIGN_SIZE 64

bitset<SIGN_SIZE> chunkToBitset(void * chunk);

Now, what I want is to create a bitset from the bits of that chunk of data (the size of the chunks is always 128 bits [16 bytes]) limited by SIGN_SIZE, where SIGN_SIZE can be any value in (32, 64, 96 or 128).

I tried to cast the chunk to unsigned long, but It does not seems to return the right value. Example:

unsigned long ulon = (unsigned long)* chunk;
bitset<SIGN_SIZE> bs(ulon);

And this approach could only work when SIGN_SIZE is 64, if it is 96 it does not work.

How can I construct the bitset with all the bits in chunk but respecting the limit SIGN_SIZE? Preferably not setting bit per bit.

Appreciate the help!

pedrostanaka
  • 711
  • 1
  • 9
  • 16
  • Loop over the data as an array of bytes, say (or `int32_t`s. or any other fixed bit size units); extract individual bits using bitwise operations; set these bits into `bitset` one by one. There's really no shortcut for bitset sizes larger than `unsigned long long`. – Igor Tandetnik Jun 21 '15 at 04:25
  • 1
    Your assignment of ulon sets it to the value of *the pointer*, not the value the pointer points to. – Some programmer dude Jun 21 '15 at 04:38
  • If `chunk` is a `char` array then `(unsigned long)*chunk` only reads the first byte of the array. You're probably looking for `*(unsigned long*)chunk`. – Jonathan Potter Jun 21 '15 at 06:10

0 Answers0