"Vince" <(E-Mail Removed)> wrote in message

news:42c02e57$0$27313$(E-Mail Removed)...

> Hi,

>

> I would like to extract or insert some values (of different size) inside

> a buffer.

> I would like to have something like this :

>

>

> BYTE Buffer[207];

>

>

> CBitAPI bitbuf<Buffer, sizeof(Buffer)>;

>

> // Extract different data

> int version = bitbuf.get(0,4); // Extract 4 bits from bit 0

> int whatever = bitbuf.get(4,2); // Extract 2 bits from bit 4

> bool ack = bitbuf.get(6,1) //Extract 1 bit from bit 6

>

> or even better :

>

> int nValue;

> bitbuf.get(4,2, nValue);

>

> DWORD dwValue;

> bitbuf.get(6,4, dwValue);

>

>

> Unfortunately I searched everywhere (boost, google, ) and I didn't found

> anything appropriate.

>

> dynamic_bitset for instance cannot be initialized from an array...

>

>
lets say you have a bit array A and you want to extract the nth bit.

Normally A would be stored as an array of bytes or ints.

|xxxxxxxx|xxxxxxxx|xxxxxxxx|xxx@xxxx|xxxxxxxx|xxxx xxxx|

lets say we want the bit at the @. First we have to figure out which byte

this is in... its pretty easy, you take the location of @ which is n(it is

28 in this example) and divide by 8 and that gives you what "bin" you are

in(i.e., what byte @ belongs to)

to find out what how "far" into the byte @ is in, we just have to compute

n - 8*floor(n/

.

8*floor(n/

represents what the location of the byte that @ is in in terms

of bits.

n - 8*floor(n/

is the same as n mod 8.

so, the simple formula to extract the byte that contains the bit is

(floor(n/

means n / 8 in integer division)

OurByte = BitArray[floor(n/

];

Now to get the bit we simply have to do this

((1 << (n %

) & OurByte) >> (n %

You should learn bitwise arithmetic to understand that. I'm sure there are

many tutorials online

note that

1 << (n %

has a 1 in the position of the location of @ and 0's everywhere

else

so 1 << (28 %

= 1 << 4 = 00010000 in the case above (note the 1 is at the

4th bit (this is what (1 << x) means... means we put a 1 at the xth bit)

00010000 & xxx@xxxx = 000@0000

and the << shifts the bit back to the first location..

000@0000 >> (28 %

= 000@0000 >> 4 = @0000000 = @

Now, I'm sure you can find some stuff online that can make it clearer if you

don't understand it.

Two things to note: You might want to use ints instead of bytes... I think

you should be ablet o figure out the obvious generalization. Second is that

this has to be modified on different architectures because of the way

certain data types or stored(look for endianess)

Hope that helps some.

Jon