Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Extract bits from byte array

Thread view: 
andrinarenee@gmail.com - 26 Jun 2006 16:52 GMT
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.
Andrey Kuznetsov - 26 Jun 2006 17:23 GMT
> 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
[quoted text clipped - 5 lines]
>
> 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

Signature

http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.