How to present it in Java

typedef struct _dmk {
       unsigned short int m     : 5;    // 0 - 31  
       unsigned short int d     : 5;    // 0 - 31  
       unsigned short int c     : 1;    // 0 - 1   
       unsigned short int i     : 5;    /* 0 - 31 */
       unsigned short int ip    : 10;   /* 0 - 1024 */
       unsigned short int mj    : 1;    // 0 - 1
       unsigned short int       : 5;    /* unused */ 
       char    msk[10];
    } DMSK;

What is represented here :? Should I use byte data type or short will be okay? Also, the last unsigned short int declaration does not specify a variable name. What does it mean? What is the meaning of 5, 5, 1, 5 ....? Please explain. thank

+3
source share
6 answers

Just use the Java method, for example getM()and setM(). Of course you have to write teir code.

Your structure describes a bitmap. The first 5 bits contain the field; the mnext 5 bits (crossing the byte boundary) contain d, etc.

JFC (Java API) , , , , SlidingInteger, . :

class DMK {
    private static final int FIELD_M = 0;
    private static final int FIELD_D = 1;
    private static final int FIELD_C = 2;
    private static final int FIELD_I = 3;
    private static final int FIELD_IP = 4;
    private static final int FIELD_MJ = 5;
    private static final int FIELD_PLACEHOLDER1 = 6;

    private SlidingInteger[] fields;

    public DMK() {
        fields = new SlidingInteger[7];
        fields[FIELD_M] = new SlidingInteger(5);
        fields[FIELD_D] = new SlidingInteger(5);
        fields[FIELD_C] = new SlidingInteger(1);
        fields[FIELD_I] = new SlidingInteger(5);
        fields[FIELD_IP] = new SlidingInteger(10);
        fields[FIELD_MJ] = new SlidingInteger(1);
        fields[FIELD_PLACEHOLDER1] = new SlidingInteger(1);
    }

    public int getM() {
        return fields[FIELD_M].getIntValue();
    }

    public int setM(int newVal) {
        fields[FIELD_M].setIntValue(newVal);
    }

    public int getD() {
        return fields[FIELD_D].getIntValue();
    }

    public int setD(int newVal) {
        fields[FIELD_D].setIntValue(newVal);
    }
}
+1

C. Java. , int.

+2

, unsigned int.

0

m, d, c, i, mj (8 ) ( c ), ip ( 16 ).

0

, , .

You will need to use the following larger types with a width of 8 or 16 bits.

An element without a name is just explicit filling. The number of specified bits is skipped, and in reality this is not necessary, since the next element will be byte aligned anyway.

0
source

The Struct class from the Javolution library does what you need ( http://www.javolution.org/apidocs/index.html?javolution/io/Struct.html ) See the 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();
 }
0
source

All Articles