如何在 Java 中表示这一点

发布于 2024-11-04 09:37:25 字数 567 浏览 0 评论 0原文

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;

: 这里代表什么?我应该使用字节数据类型还是短数据类型就可以了? 另外,在最后一个 unsigned Short int 声明中,没有指定变量名。这意味着什么? 5、5、1、5......有什么意义?请解释一下。谢谢

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 . What does that mean? What is the significance of 5 , 5 , 1, 5....? Please explain. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

久而酒知 2024-11-11 09:37:26

这些是 C 语言中的位字段。这种结构几乎不可能像 Java 中那样表示。尽管您可以只公开底层 int,但您必须编写方法来访问各个位。

Those are bit fields in C. This structure's going to be nearly impossible to represent as is in Java. You'd have to write methods to access the individual bits, although you could just expose the underlying int.

烟燃烟灭 2024-11-11 09:37:26

只需使用 Java 方式,例如 getM()setM()。当然你必须编写他们的代码。

您的结构描述了一个位表。前 5 位包含字段 m,接下来的 5 位(跨越字节边界)包含 d 等等。

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

Just use the Java way, like getM() and setM(). Of course you have to write teir code.

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.

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. 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);
    }
}
深陷 2024-11-11 09:37:26

它表示unsigned int 占用的位数。

It denotes the number of bits the unsigned int takes.

丶视觉 2024-11-11 09:37:26

对于 m、d、c、i、mj,您可以使用 byte(最多 8 位)数据类型(您也可以使用 c 作为布尔值),但 ip 至少需要一个 Short(最多 16 位)。

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).

Oo萌小芽oO 2024-11-11 09:37:26

这是一种将数据打包到特定位数的方法,也许可以节省很少的空间。

您必须使用下一个更大的类型,8 或 16 位宽。

没有名称的元素只是显式填充。指定的位数被跳过,并且这里实际上并不需要,因为无论如何下一个元素都是字节对齐的。

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.

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.

我的鱼塘能养鲲 2024-11-11 09:37:26

Javolution 库中的 Class Struct 可以满足您的需求(http://www .javolution.org/apidocs/index.html?javolution/io/Struct.html)请参阅“时钟”示例:

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

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();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文