Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a C style bitfield in Java

I have a problem that I am a bit stuck on and I was informed by a colleague that this would be a good place to seek help.

I am trying to implement a C style bitfield in Java. Here is a rough example (I do not have the actual code in front of me at this moment).

typedef union
{
  typedef struct
  {
     unsigned short a :1;
     unsigned short b :1;
     unsigned short c :2;
     unsigned short d :10;
  } bitfield;

  unsigned short bitmap;
}example_bitfield;

I have a good bit of similar style bitfields from legacy code. The reason that I need to come up with an equivalent method for Java is that I am working on code that will use Java to communicate with other legacy applications using UDP.

I do not have the option of rewriting the code. I am aware that this approach is not portable, has endianness issues (and padding/alignment, ect), and could be done a better way if I were able to rewrite the code. Unfortunately I need an answer to this very specific problem. The system is closed and so I do not need to worry about every single possible combination of compilers/operating systems/ect.

The approach of using a Java EnumSet will not work because I believe that will only allow for each value to be one bit. I need to be able to pack values with for instance the value of d occupying 10 bits.

I know about the Java Bitset but it has limitations. I am using an older version of Java, and so I do not have some of the newer Java Bitset methods (Namely the valueOf methods which would probably surely help).

Does anyone have any ideas of how to make this as manageable as possible? I have over 10 bitfields that I need to implement for my communications.

Thank you for any help you can provide!

like image 291
shadowisadog Avatar asked Sep 22 '12 17:09

shadowisadog


People also ask

What is bitfields in C?

Bit fields in C are relatively very simple than all the topics we have covered so far. A bit field is simply a data structure that helps the user to allocate memory to structures and unions. In order to understand bit fields, we need to have a clear understanding of Structures and Unions in C.

How to declare a bit-field inside a structure?

The declaration of a bit-field has the following form inside a structure − The following table describes the variable elements of a bit field − An integer type that determines how a bit-field's value is interpreted. The type may be int, signed int, or unsigned int. The name of the bit-field. The number of bits in the bit-field.

What are the limitations of bit field in C++?

2) We cannot have pointers to bit field members as they may not start at a byte boundary. 3) It is implementation defined to assign an out-of-range value to a bit field member. 4) In C++, we can have static members in a structure/class, but bit fields cannot be static. 5) Array of bit fields is not allowed.

When do we use bit field in a byte?

When devices transmit status or information encoded into multiple bits for this type of situation bit-fiels is most effiecient. Encryption routines need to access the bits within a byte in that situation bit-field is quite useful.


2 Answers

Since UDP accepts only byte arrays, you can declare a Java class in any suitable way and the only critical step is to define its serialization and deserialization methods:

class example_bitfield {
  byte a;
  byte b;
  byte c;
  short d;

  public void fromArray(byte[] m) {
    byte b0=m[0];
    byte b1=m[1];
    a=b0>>>7;
    b=(b0>>6)&1;
    c=(b0>>4)&3;
    d=(b0&0xF<<6)|(b1>>>2);
  }
  public void toArray(byte[] m) {
    m[0]=(a<<7)|(b<<6)|(c<<4)|(d>>>6);
    m[1]=(d&0x3F)<<2;
  }
}
like image 139
Alexei Kaigorodov Avatar answered Oct 21 '22 02:10

Alexei Kaigorodov


Class Struct from Javolution library makes what you need (http://www.javolution.org/apidocs/index.html?javolution/io/Struct.html) See "Clock" example:

 import java.nio.ByteBuffer;
 class Clock extends Struct { // Hardware clock mapped to memory.
     Unsigned16 seconds  = new Unsigned16(5); // unsigned short seconds:5
     Unsigned16 minutes  = new Unsigned16(5); // unsigned short minutes:5
     Unsigned16 hours    = new Unsigned16(4); // unsigned short hours:4
     Clock() {
         setByteBuffer(Clock.nativeBuffer(), 0);
     }
     private static native ByteBuffer nativeBuffer();
 }
like image 34
ant Avatar answered Oct 21 '22 02:10

ant