简体   繁体   English

如何在 Java 中表示这个

[英]How to represent this 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 does the: represent here?在这里代表什么? Should I use byte data type or short will be fine?我应该使用字节数据类型还是短格式就可以了? Also in the last unsigned short int declaration there is no variable name specified.同样在最后一个 unsigned short int 声明中没有指定变量名。 What does that mean?这意味着什么? What is the significance of 5, 5, 1, 5....? 5、5、1、5....的意义是什么? Please explain.请解释。 Thanks谢谢

Those are bit fields in C.这些是 C 中的位字段。 This structure's going to be nearly impossible to represent as is in Java.这种结构几乎不可能像 Java 中那样表示。 You'd have to write methods to access the individual bits, although you could just expose the underlying int.您必须编写方法来访问各个位,尽管您可以只公开底层 int。

Just use the Java way, like getM() and setM() .只需使用 Java 方式,如getM()setM() Of course you have to write teir code.当然,您必须编写 teir 代码。

Your structure describes a bit table.您的结构描述了一个位表。 The first 5 bits contain the field m the next 5 bits (crossing the byte boundary) contains d and so on.前 5 位包含字段m ,接下来的 5 位(跨越字节边界)包含d ,依此类推。

JFC (the Java API) has no implementation which can help you, so if you use structures like this often in your program I recommend writing a class like SlidingInteger which can handle a single field. JFC(Java API)没有可以帮助你的实现,所以如果你在你的程序中经常使用这样的结构,我建议你编写一个 class 像SlidingInteger这样可以处理单个字段。 Like this:像这样:

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);
    }
}

Class Struct from Javolution library makes what you need ( http://www.javolution.org/apidocs/index.html?javolution/io/Struct.html ) See "Clock" example: 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();
 }

It denotes the number of bits the unsigned int takes.它表示unsigned int占用的位数。

For m, d, c, i, mj you can use byte (8bit max) data type (you can also use c as a boolean), but ip requires at least a short (16bit max).对于 m、d、c、i、mj,您可以使用字节(最大 8 位)数据类型(您也可以使用 c 作为布尔值),但 Z957B527BCFBAD2E2E80F58ZD2068 至少需要 8 位

This is a way to pack data to specific number of bits, to perhaps save a very small amount of space.这是一种将数据打包到特定位数的方法,也许可以节省非常少的空间。

You would have to use the next larger types, 8 or 16 bits wide.您将不得不使用下一个更大的类型,8 位或 16 位宽。

The element without a name is just explicit padding.没有名称的元素只是显式填充。 The number of bits specified is skipped, and not really needed here as the next element would be byte aligned anyway.指定的位数被跳过,这里并不真正需要,因为下一个元素无论如何都是字节对齐的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM