如何检查位字段顺序。 [英] How to check bit fields order.

查看:79
本文介绍了如何检查位字段顺序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好 - 我正在阅读K& R C书(第150页),其中(位)字段在某些机器上从左到右分配,在其他机器上从右到左分配。这意味着列出从最重要到最小的位字段,反之亦然。


所以我为包含所有相关位域结构的驱动程序创建了几个头文件...我需要测试我公司的编译器如何命令位字段。


我正在考虑创建一个文件,其中一个结构列出了位字段......并对其进行了测试。但是如何调用它,并检查第一位或最后一位是否已读(我也是C的新手)请帮助!谢谢!

Hi everyone - I was reading in the K&R C book (on page 150) that (Bit) Fields are assigned left to right on some machines and right to left on others. This means listing the bit fields from most significant to least, or vice versa.

So I am creating a couple header files for drivers which contain structures of all the relevant bit fields....I need to "test" how my company''s compiler orders the bit fields.

I was thinking about creating a file with one of the structures which lists the bit fields...and testing that. But how do I call it, and check that the first or last bit was read (I am also new to C) Please help! thanks!

推荐答案


我需要测试我公司的编译器如何命令位字段。
I need to "test" how my company''s compiler orders the bit fields.



为什么您认为有必要准确了解编译器如何排序位字段?如果你小心编写便携式代码,那么你对位字的排序方式没有意义。


我能想到的例外情况:

1.您可能正在使用位字段覆盖内存映射的I / O寄存器。显然,除非您知道编译器如何命令位字段,否则您无法确定此策略是否有效。这就是我建议你不要以这种方式使用位字段的确切原因 - 改为使用位掩码。


2.你正在编写一个结构(包含位字段)到另一台机器可能使用具有不同位字段排序规则的不同编译器读取的文件。您应该仔细而完整地定义可移植数据文件的格式,而无需参考C / C ++关键字。然后编写可以创建符合该规范的数据文件的便携式软件。

Why do you feel it is necessary to know precisely how bit fields are ordered by your compiler? If you''re careful to write portable code then it won''t matter to you how bit fields are ordered.

Exceptions I can think of:

1. You might be using bit fields to overlay a memory-mapped I/O register. Obviously, you can''t be certain this strategy works unless you know how the compiler orders the bit fields. That''s the precise reason why I advise you not to use bit fields in this way -- use bit masks instead.

2. You''re writing a structure (that contains bit fields) to a file that might be read by another machine using a different compiler with different bit field ordering rules. You should define the format of portable data files carefully and completely without any reference to C/C++ keywords. Then write portable software that creates a data file conforming to that specifications.


当我认为编译器时,我可能会误认为是错误的。命令位字段。无论如何 - 他们需要在订单中,因为他们正在映射硬件寄存器......请帮助。谢谢!
In all possibility I could be mistaken when I think that the "compiler" orders the bit fields. Anyway - they need to be in an order, since they are mapping the hardware registers...please help. thanks!


确定位域的顺序


方法1


RTFM!毫无疑问,您的平台将拥有平台指南(或用户指南),这是它包含的信息以及诸如字节序,字符的默认符号,处理器位大小和其他重要内容(如果您正好在写作)之类的内容非便携式代码)


方法2


创建一个包含位域的结构,向它们写0,然后写1到其中之一。在下面的代码行中加上一个断点并在平台调试器中运行程序。


方法3


不要打扰,做上面1中所述的donblock使用位掩码来访问你的硬件寄存器它是可移植的。
Determining the Order of Bit Fields

Method 1

RTFM! Your platform will undoubtedly have a platform guide (or user guide) and this is the sort of information it contains as well as such things as endianess, default sign of a char, processor bit size and other important stuff (if you happen to be writing non-portable code)

Method 2

Create a structure with bit fields in them, write 0 to them all, then write 1 to one of them. Put a break point on the following line of code and run the program in the platforms debugger.

Method 3

Don''t bother, do what donblock said in 1 above use bit masks to access your hardware registers it is portable.


这篇关于如何检查位字段顺序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆