在C ++中定义一个字节 [英] Defining a byte in C++

查看:243
本文介绍了在C ++中定义一个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.parashift.com /c++-faq-lite/intrinsic-types.html#faq-26.6 ,它是无效的

另一个有效的方法是定义一个字节为9位,并模拟一个char *由两个字的内存:第一个可能指向36位字,第二个可能是该字内的位偏移在这种情况下,C ++编译器将需要添加额外的指令,当使用char *指针。

In http://www.parashift.com/c++-faq-lite/intrinsic-types.html#faq-26.6, it is wriiten that
"Another valid approach would be to define a "byte" as 9 bits, and simulate a char* by two words of memory: the first could point to the 36-bit word, the second could be a bit-offset within that word. In that case, the C++ compiler would need to add extra instructions when compiling code using char* pointers."

我无法理解用两个字模拟char *的含义,并进一步引用。

有人请解释一下。一个例子?

I couldn't understand what it meant by "simulating char* by two words" and further quote.
Could somebody please explain it by giving an example ?

推荐答案

我认为这是他们描述的内容:

I think this is what they were describing:

第二段中提及的PDP-10具有36位字,并且不能对那些字内的任何内容进行寻址。以下文本描述了在C ++语言规范(包含在第一段中)的限制范围内可以解决此问题的一种方法。

The PDP-10 referenced in the second paragraph had 36-bit words and was unable to address anything inside of those words. The following text is a description of one way that this problem could have been solved while fitting within the restrictions of the C++ language spec (that are included in the first paragraph).

让我们假设你想做9位长字节(由于某种原因)。根据规范, char * 必须能够寻址单个字节。 PDP-10不能这样做,因为它不能处理小于36位字的任何东西。

Let's assume that you want to make 9-bit-long bytes (for some reason). By the spec, a char* must be able to address individual bytes. The PDP-10 can't do this, because it can't address anything smaller than a 36-bit word.

PDP-10的局限性之一是使用两个内存字符来模拟 c $ * 。第一个字将是指向包含char的36位字的指针(这通常与PDP-10的指针允许的精确一样)。第二个字将指示该字内的偏移(以位为单位)。现在, char * 可以访问系统中的任何字节,并符合C ++规范的限制。

One way around the PDP-10's limitations would be to simulate a char* using two words of memory. The first word would be a pointer to the 36-bit word containing the char (this is normally as precise as the PDP-10's pointers allow). The second word would indicate an offset (in bits) within that word. Now, the char* can access any byte in the system and complies with the C++ spec's limitations.

艺术视觉辅助:

| Byte 1 | Byte 2 | Byte 3 | Byte 4 | Byte 5 | Byte 6 | Byte 7 | Byte 8 |
-------------------------------------------------------------------------
|               Word 1              |               Word 2              |
|              (Address)            |              (Offset)             |
-------------------------------------------------------------------------

假设你有一个 char * word1 = 0x0100 word2 = 0x12 。这将指向存储器的第256个字的第18位(第三个字节的开始)。

Say you had a char* with word1 = 0x0100 and word2 = 0x12. This would point to the 18th bit (the start of the third byte) of the 256th word of memory.

如果这种技术真的用于生成一个符合的C ++实现PDP-10,然后C ++编译器将不得不做一些额外的工作,杂耍这个相当有趣的内部格式所需的额外的位。

If this technique was really used to generate a conforming C++ implementation on the PDP-10, then the C++ compiler would have to do some extra work with juggling the extra bits required by this rather funky internal format.

这篇文章的整个要点是为了说明 char 不总是8位。它至少 8位,但没有定义的最大值。数据类型的内部表示取决于平台架构,并且可能与您期望的不同。

The whole point of that article is to illustrate that a char isn't always 8 bits. It is at least 8 bits, but there is no defined maximum. The internal representation of data types is dependent on the platform architecture and may be different than what you expect.

这篇关于在C ++中定义一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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