bits operation

V

Vince

I would like to know how I can extract some data from a byte array.
For example let's say I have a byte array with 4 elements ie 32 bits.


BYTE buf[4];

I fill buf with the following values : 00011{100 1}1110101 10100101
00000001.

Now I want to get the values between bracket.
How can I do that ?
Is Bitset appropriate knowing that in reality my array is 29 bytes long
and I want to extract 1-32 bits from a defined offset.
 
I

Ivan Vecerina

Vince said:
I would like to know how I can extract some data from a byte array.
For example let's say I have a byte array with 4 elements ie 32 bits.

BYTE buf[4];

I fill buf with the following values : 00011{100 1}1110101 10100101
00000001.

Now I want to get the values between bracket.
How can I do that ?
Is Bitset appropriate knowing that in reality my array is 29 bytes long
and I want to extract 1-32 bits from a defined offset.
Yes, on a 32-bit platform, with bitset you should be able to easily
extract bits using something like:
(myBitSet>>bitOffset).to_ulong() & ((1UL<<numBits)-1);
(NB: you need to special-case the case where numBits is 32!)


hth -Ivan
 
R

raj

BYTE buf[4];
I fill buf with the following values : 00011{100 1}1110101 10100101
00000001.

Now I want to get the values between bracket.
How can I do that ?

You could do that with ">>" , "<<" operators

Raj
 
I

Ioannis Vranos

Vince said:
I would like to know how I can extract some data from a byte array.
For example let's say I have a byte array with 4 elements ie 32 bits.


BYTE buf[4];

I fill buf with the following values : 00011{100 1}1110101 10100101
00000001.

Now I want to get the values between bracket.
How can I do that ?
Is Bitset appropriate knowing that in reality my array is 29 bytes long
and I want to extract 1-32 bits from a defined offset.


You will use two bitsets where you will assign buf[0] and buf[1] and get
the individual bits you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top