如何在 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;
: 这里代表什么?我应该使用字节数据类型还是短数据类型就可以了? 另外,在最后一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这些是 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.
只需使用 Java 方式,例如
getM()
和setM()
。当然你必须编写他们的代码。您的结构描述了一个位表。前 5 位包含字段
m
,接下来的 5 位(跨越字节边界)包含d
等等。JFC(Java API)没有可以帮助您的实现,因此如果您在程序中经常使用这样的结构,我建议您编写一个像
SlidingInteger
这样可以处理单个字段的类。像这样:Just use the Java way, like
getM()
andsetM()
. 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) containsd
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:它表示
unsigned int
占用的位数。It denotes the number of bits the
unsigned int
takes.对于 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).
这是一种将数据打包到特定位数的方法,也许可以节省很少的空间。
您必须使用下一个更大的类型,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.
Javolution 库中的 Class Struct 可以满足您的需求(http://www .javolution.org/apidocs/index.html?javolution/io/Struct.html)请参阅“时钟”示例:
Class Struct from Javolution library makes what you need (http://www.javolution.org/apidocs/index.html?javolution/io/Struct.html) See "Clock" example: