java - JNA:如何定义具有自定义位大小字段的结构?

标签 java struct structure jna bit

例如,我有以下 C++ 结构...

struct dleaf_t
{
    int                 contents;           // OR of all brushes (not needed?)
    short               cluster;            // cluster this leaf is in
    short               area : 9;           // area this leaf is in
    short               flags : 7;          // flags
    short               mins[ 3 ];          // for frustum culling
    short               maxs[ 3 ];
    unsigned short      firstleafface;      // index into leaffaces
    unsigned short      numleaffaces;
    unsigned short      firstleafbrush;     // index into leafbrushes
    unsigned short      numleafbrushes;
    short               leafWaterDataID;    // -1 for not in water

    //!!! NOTE: for maps of version 19 or lower uncomment this block
    /*
    CompressedLightCube ambientLighting;    // Precaculated light info for entities.
    short           padding;        // padding to 4-byte boundary
    */
};

通常,我可以使用 JNA 轻松地在 Java 中表示结构,但此结构使用数据类型的自定义位数(如果我没记错的话)。

例如,area9 位的short,而不是像通常那样的 16 位。 flags 缺少 7 位...等等。

如何使用自定义位大小的数据类型定义 JNA 结构?

最佳答案

将两个位字段合并为一个 int 字段,然后编写成员方法来提取已组合的各个字段的值,例如

public int area_flags;
public int getArea() { return area_flags & 0x1FF; }
public int getFlags() { return (area_flags >> 9) & 0x3F; }

您可能需要稍微尝试一下,具体取决于您的编译器如何打包这些位。

关于java - JNA:如何定义具有自定义位大小字段的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38387638/

相关文章:

c - 如何直接分配在结构内声明的整数数组?

Java ArrayList 正在发挥作用

java - 如何访问存储在在线文件中的变量

使用 dirent 计算 C 中目录中的文件数第二次不起作用。

c# - 从顺序文件读取到结构数组

具有嵌套 map golang 的结构

java - 从 javax.persistence.EntityManager 获取 javax.sql.DataSource

带有代理的 Java 11 HttpClient, header 解析器未收到任何字节

math - Monad 有什么特别之处?

c++ - 获取函数成员地址