LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 702|回复: 5

6 bits data type

[复制链接]
发表于 2003-11-15 15:58:53 | 显示全部楼层 |阅读模式
请问要如何才能做一个只用6 bits的data type?
而且要写进binary文件里
发表于 2003-11-15 17:34:32 | 显示全部楼层
struct my_type{
    int _6bit:6
}__attribute__ ((__packed__));
发表于 2003-11-15 20:41:14 | 显示全部楼层
其实来是用的是八位的,一个字节的,
要是想一个六位接一个六位的,只能自己分析了吧。
如,第一个字节的前六位加上,后二位,加上第二个字节的前四位,这样子与或吧。
 楼主| 发表于 2003-11-16 05:04:00 | 显示全部楼层
谢谢两位大侠指点~
_z_ 大侠
这个也算是bit field 吧?
那要怎么写入bianry文件?
如果用 fwrite()的话, sizeof(my_type) 返回值还是 1, 那我每次写进去的还是 1byte 而不是 6 bits...@_@

还可以解释一下__attribute__ ((__packed__)); 是什么吗?

为什么
struct my_type{
float _24bit : 24 ;
}__attribute__ ((__packed__)); 就不行呢?
发表于 2003-11-16 09:42:02 | 显示全部楼层
i86寻址单位是字节,用位应该是不行了。
可以把这个数据结构与其它数据结构组合使用。

如:
#pragma options align=packed
struct my_type{
int _6bit:6
};
#pragma options align=reset
/* other structures */

__attribute__ ((__packed__));防止编译器进行对齐优化
 楼主| 发表于 2003-11-17 15:22:09 | 显示全部楼层
谢谢~~
学到东西了..
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表