本文介绍了在位字段的情况下,哪一个是更好地使用,unsigned char或unsigned int和为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解一下下面的结构声明。哪一个是更好地使用内存分配,为什么?并且怎么样在unsigned char型和unsigned int的情况下,填充?

 结构数据{
 unsigned char型一:3;
 unsigned char型乙:4;
};

 结构数据{
 unsigned int类型一:3;
 unsigned int类型A:4;
};


解决方案

位域应与类型声明符号int 无符号整型。其他类型的可以或可以不被支持。

C标准中,只有无符号(INT)和INT是一个位域成员可以接受的数据类型。一些编译器允许无符号的字符.......

I just want to know about following structure declarations. Which one is better to use for memory allocation and why? And what about padding in case of unsigned char and unsigned int?

struct data{
 unsigned char a:3;
 unsigned char b:4;
}; 

and

struct data{
 unsigned int a:3;
 unsigned int b:4;
};
解决方案

Bit fields should be declared with type signed int, unsigned int. Other types may or may not be supported.

From Atmel

in the C Standard, only "unsigned (int)" and "int" are acceptable datatypes for a bitfield member. Some compilers allow "unsigned char" .......

这篇关于在位字段的情况下,哪一个是更好地使用,unsigned char或unsigned int和为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:22