如何在 C 中处理位数据
在课堂上,我的任务是编写一个 C 程序来解压缩文本文件并打印出其中包含的字符。文件中的每个字符由 2 位(4 个可能的字符)表示。
我最近获悉,在所有系统上,一个字节不一定是 8 位,一个 char
不一定是 1 个字节。这让我想知道当我加载 1 个字节时,我到底如何知道从文件中加载了多少位。另外,当没有可以保证一定数量位的数据类型时,我应该如何将加载的数据保留在内存中。
如何在 C 中处理位数据?
In class I've been tasked with writing a C program that decompresses a text file and prints out the characters it contains. Each character in the file is represented by 2 bits (4 possible characters).
I've recently been informed that a byte is not necessarily 8 bits on all systems, and a char
is not necessarily 1 byte. This then makes me wonder how on earth I'm supposed to know how many bits got loaded from a file when I loaded 1 byte. Also how am I supposed to keep the loaded data in memory when there are no data types that can guarantee a set amount of bits.
How do I work with bit data in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个字节不一定是 8 位。这确实是事实。另一方面,char 被定义为 byte - C 不区分这两者。
然而,您要为其编写的系统几乎肯定将具有 8 位字节。在非常非常旧的系统或某些嵌入式系统之外,不同大小的字节基本上不存在。
如果您必须编写适用于多个平台的代码,并且其中一个或多个具有不同大小的字符,那么您可以专门编写代码来处理该平台 - 例如使用 CHAR_BIT 来确定每个字节包含多少位。
鉴于这是一个类,假设 8 位字节,除非另有说明。重点不是极端的平台独立性,重点是教你一些关于位摆弄的东西(或者可能是位字段,但这取决于你在课堂上学到的内容)。
A byte is not necessarily 8 bits. That much is certainly true. A char, on the other hand, is defined to be a byte - C does not differentiate between the two things.
However, the systems you will write for will almost certainly have 8-bit bytes. Bytes of different sizes are essentially non-existant outside of really, really old systems, or certain embedded systems.
If you have to write your code to work for multiple platforms, and one or more of those have differently sized chars, then you write code specifically to handle that platform - using e.g. CHAR_BIT to determine how many bits each byte contains.
Given that this is for a class, assume 8-bit bytes, unless told otherwise. The point is not going to be extreme platform independence, the point is to teach you something about bit fiddling (or possibly bit fields, but that depends on what you've covered in class).
您将很难找到一个字节不是 8 位的平台。 (尽管如上所述,可以使用 CHAR_BIT 来验证这一点)。 还要与您的讲师澄清可移植性要求或陈述您的假设。
通常使用移位和按位运算来提取位,例如
(x & 3)
是最右边的 2 位x。((x>>2) & 3)
是接下来的两位。为您的目标平台选择正确的数据类型,或者正如其他人所说,使用 uint8_t 之类的数据类型(如果您的编译器可用)。另请参阅:
用于表示字节的类型ANSI (C89/90) C?
我建议不要使用位字段。另请参阅此处:
何时值得使用位字段?
You'll be hard pressed to find a platform where a byte is not 8 bits. (though as noted above CHAR_BIT can be used to verify that). Also clarify the portability requirements with your instructor or state your assumptions.
Usually bits are extracted using shifts and bitwise operations, e.g.
(x & 3)
are the rightmost 2 bits of x.((x>>2) & 3)
are the next two bits. Pick the right data type for the platforms you are targettiing or as others say use something like uint8_t if available for your compiler.Also see:
Type to use to represent a byte in ANSI (C89/90) C?
I would recommend not using bit fields. Also see here:
When is it worthwhile to use bit fields?
您可以在 C 中使用位字段。如果您真正关心宽度,这些索引可以明确指定字段每个部分的位数。此页面提供了讨论:http://msdn。 microsoft.com/en-us/library/yszfawxh(v=vs.80).aspx
作为示例,请查看
ieee754.h
的使用情况实现 IEEE754 浮点的上下文You can use bit fields in C. These indices explicitly let you specify the number of bits in each part of the field, if you are truly concerned about width. This page gives a discussion: http://msdn.microsoft.com/en-us/library/yszfawxh(v=vs.80).aspx
As an example, check out the
ieee754.h
for usage in the context of implementing IEEE754 floats