本文介绍了C ++ 14中二进制文字的字节顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我尝试四处搜索,但找不到太多有关二进制文字和字节序的信息。二进制文字是小尾数,大尾数还是其他东西(例如匹配目标平台)?

I have tried searching around but have not been able to find much about binary literals and endianness. Are binary literals little-endian, big-endian or something else (such as matching the target platform)?

例如,<$ c的十进制值是多少$ c> 0b0111 ?是7吗?平台特定?还有吗编辑:我选择了一个错误的值7,因为它在一个字节内表示。

As an example, what is the decimal value of 0b0111? Is it 7? Platform specific? Something else? I picked a bad value of 7 since it is represented within one byte. The question has been sufficiently answered despite this fact.

某些背景:基本上我想弄清楚最低有效位的值是什么,并用它掩盖二进制字面量似乎是一个不错的选择...但是只有在有关字节序的某些保证下。

Some background: Basically I'm trying to figure out what the value of the least significant bits are, and masking it with binary literals seemed like a good way to go... but only if there is some guarantee about endianness.

推荐答案

简短答案: 没有一个人

长答案:
Endianness永远不会直接出现在代码中,除非您真正尝试将其取出(例如使用指针技巧)。 0b0111 为7,与十六进制相同,即写

Long answer:Endianness is never exposed directly in the code unless you really try to get it out (such as using pointer tricks). 0b0111 is 7, it's the same rules as hex, writing

int i = 0xAA77;

在某些平台上并不表示 0x77AA ,因为那样会荒诞。 32位整数会在哪里丢失丢失的额外0?他们会在前面被填充,然后整个事情变成 0x77AA0000 ,还是会在后面被添加?我不知道有人会想到这种情况。

doesn't mean 0x77AA on some platforms because that would be absurd. Where would the extra 0s that are missing go anyway with 32-bit ints? Would they get padded on the front, then the whole thing flipped to 0x77AA0000, or would they get added after? I have no idea what someone would expect if that were the case.

要点是,如果您使用原语编写代码,则C ++不会对机器的字节序进行任何假设*。以及它提供的文字,机器之间的行为是相同的(除非您开始规避类型系统,否则可能需要这样做)。

The point is that C++ doesn't make any assumptions about the endianness of the machine*, if you write code using primitives and the literals it provides, the behavior will be the same from machine to machine (unless you start circumventing the type system, which you may need to do).

更新:数字将是您将其写入的方式。这些位将不会重新排序或其他任何事情,最高有效位在左侧,最低有效位在右侧。

To address your update: the number will be the way you write it out. The bits will not be reordered or any such thing, the most significant bit is on the left and the least significant bit is on the right.

这里的字节序似乎存在误解。字节顺序是指字节在内存中的排序方式以及必须如何解释它们。如果我给您编号 4172,并说如果是四千一百七十二,那么字节序是什么。您真的无法给出答案,因为问题没有道理。 (有人争辩说,左边最大的数字表示尾数大,但没有记忆就解决了尾数问题是不负责任的或不相关的)。这只是一个数字,没有要解释的字节,没有内存地址。假定为4字节整数表示形式,对应于它的字节为:

There seems to be a misunderstanding here about what endianness is. Endianness refers to how bytes are ordered in memory and how they must be interpretted. If I gave you the number "4172" and said "if this is four-thousand one-hundred seventy-two, what is the endianness" you can't really give an answer because the question doesn't make sense. (some argue that the largest digit on the left means big endian, but without memory addresses the question of endianness is not answerable or relevant). This is just a number, there are no bytes to interpret, there are no memory addresses. Assuming 4 byte integer representation, the bytes that correspond to it are:

        low address ----> high address
Big endian:    00 00 10 4c
Little endian: 4c 10 00 00

so ,给出其中任何一个并告诉这是计算机的内部表示4172。

so, given either of those and told "this is the computer's internal representation of 4172" you could determine if its little or big endian.

因此,现在考虑您的二进制文字 0b0111 这4位代表一个半字节,并且可以

So now consider your binary literal 0b0111 these 4 bits represent one nybble, and can be stored as either

              low ---> high
Big endian:    00 00 00 07
Little endian: 07 00 00 00

但是不必担心,因为这也是由硬件处理的,该语言指示编译器从左到右读取,从最高有效位到最低有效位

But you don't have to care because this is also handled by the hardware, the language dictates that the compiler reads from left to right, most significant bit to least significant bit

Endianness是无关紧要。给定一个字节是8位,如果我递给您 0b00000111 并说这是大端还是小端?同样,您不能说是因为您只有一个字节(没有地址)。字节顺序与字节中的位顺序无关,它是指整个字节相对于地址的顺序(当然,除非您有一个位字节)。

Endianness is not about individual bits. Given that a byte is 8 bits, if I hand you 0b00000111 and say "is this little or big endian?" again you can't say because you only have one byte (and no addresses). Endianness doesn't pertain to the order of bits in a byte, it refers to the ordering of entire bytes with respect to address(unless of course you have one-bit bytes).

您不必担心计算机内部使用的是什么。 0b0111 只是节省了您不必编写

You don't have to care about what your computer is using internally. 0b0111 just saves you the time from having to write stuff like

unsigned int mask = 7; // only keep the lowest 3 bits

通过写入

unsigned int mask = 0b0111;

无需评论说明数字的含义。

Without needing to comment explaining the significance of the number.

*在c ++ 20中,您可以使用。

* In c++20 you can check the endianness using std::endian.

这篇关于C ++ 14中二进制文字的字节顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:08