Extract bits from byte array

A

andrinarenee

Hello,

I will have an array of 48 byte that will correspond to 48 different
registers. Each register has different fields (eg. register 1 has 1
field of 2 bits, and another field of 6 bits, register 2 is composed of
1 2-bit field and 6 1-bit fields.)

How can I parse the byte array and extract the bits corresponding to
the different fields (and field lengths)

Any suggestions are greatly appreciated.
 
A

Andrey Kuznetsov

I will have an array of 48 byte that will correspond to 48 different
registers. Each register has different fields (eg. register 1 has 1
field of 2 bits, and another field of 6 bits, register 2 is composed of
1 2-bit field and 6 1-bit fields.)

How can I parse the byte array and extract the bits corresponding to
the different fields (and field lengths)

Any suggestions are greatly appreciated.

you need three things to do it:
1) index in byte array
2) bit mask
3) shift amount


class Register {

byte [] values;

int index;
int shift;
int mask;

public int getValue() {
return (values[index] & mask) >> shift ;
}
}

Andrey
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top