简体   繁体   English

位排序和结束

[英]bit ordering and endianess

I am reading a file byte-by-byte. 我正在逐字节地读取文件。

Say for example i have this byte: 0x41 (0100 0001) represented in hex. 比如说我有这个字节:0x41(0100 0001)用十六进制表示。

Now, I want the first three bits of this byte, ie (010). 现在,我想要这个字节的前三位,即(010)。

I can use bitwise logic to extract the first three bits, but my question is will the first three bits be independent of endianess of the machine.(ie they can't be 001)? 我可以使用按位逻辑来提取前三位,但我的问题是前三位是否与机器的endianess无关(即它们不能是001)?

Thanks, 谢谢,

Endianness only applies to byte order, not bit order. 字节顺序仅适用于字节顺序,而不适用于位顺序。 The order of bits will be the same within the corresponding byte. 位的顺序在相应的字节内是相同的。

Another way to think of this is that endianness only applies when you can read the components of an item individually - since you can typically independently read from memory the individual bytes of a 32-bit int, if you want to interpret those bytes as a 32-bit int you need ot ensure that the endianess of the architecture is taken into consideration. 想到这一点的另一种方法是只有在你可以单独读取项目的组件时才会应用字节顺序 - 因为你通常可以独立地从内存中读取32位int的各个字节,如果你想将这些字节解释为32 -bit int您需要确保考虑架构的endianess。

Normally you can't read from memory the individual bits of a byte, so there really is no concept of 'bit endianness' as far as memory architecture is concerned (I'm sure there is at the hardware level, but not something you can see at the software level). 通常你不能从内存中读取一个字节的各个位,所以就存储器架构而言,确实没有“位字节式”的概念(我确信存在硬件级别,但不是你能做到的事情)看看软件层面)。 A few areas where you might need to deal with (or at least be aware of) bit endianness: 您可能需要处理(或至少知道)位字节序的一些区域:

  1. the order that the compiler stores bits of a bit field is compiler dependent (and is not necessarily related to the endianess of the hardware platform - different compilers might order bit fields differently for the same platform - it's possible that a compiler could be configured one way or the other using command line options, similar to the way char might be set to be signed or unsigned). 编译器存储位字段位的顺序取决于编译器(并且不一定与硬件平台的字节顺序相关 - 不同的编译器可能会为同一平台不同地排序位字段 - 编译器可能是单向配置的或者另一个使用命令行选项,类似于char可以设置为有符号或无符号的方式)。 However, C bit fields really have nothing to do with hardware addressing. 但是,C位字段实际上与硬件寻址无关。

  2. some hardware architectures do allow you to address individual bits (the ARM Cortex M3 for example), so in this case you'd need to know how the architecture arranged for bits to be addressed if you were to use that feature. 某些硬件架构允许您处理单个位(例如ARM Cortex M3),因此在这种情况下,您需要知道如果要使用该功能,架构如何安排用于寻址的位。

  3. if you're sending bits over a serial link - the hardware interface will typically specify whether the most significant bit or least significant bit is 'shifted' out on the wire first. 如果您通过串行链路发送位 - 硬件接口通常会指定最高有效位或最低有效位是否先在线路上“移位”。

Yes, they will be the same. 是的,他们会是一样的。

Bit ordering inside bytes is generally only an issue when you're doing bit-by-bit I/O for instance when reading a stream of data sent over a serial line. 在读取通过串行线发送的数据流时,例如当您进行逐位I / O时,字节内的位排序通常只是一个问题。 Those really send a single bit at a time, so sender and receiver need to agree if bits are sent left-to-right or right-to-left, for each byte. 那些确实一次发送一个比特,所以发送者和接收者需要同意每个字节是从左到右还是从右到左发送比特。

For files, and in-memory accesses, bit ordering inside bytes does not change. 对于文件和内存中访问,字节内的位排序不会改变。

Bit order matters when a field uses part of a byte, or spans bytes starting or ending (or both) part way through a byte. 当字段使用字节的一部分时,或者跨越字节开始或结束(或两者)的字节顺序时,位顺序很重要。

Example: 2 bytes of data first 235 (decimal) second 173 (decimal), aka hex EB and AD. 示例:2字节数据先235(十进制)秒173(十进制),又名十六进制EB和AD。

I want a bit field beginning at the fourth bit, through the 12th bit. 我希望从第4位开始到第12位的位域。 So, skip over 3 bits, make a 9 bit unsigned integer from the next 9 bits. 因此,跳过3位,从接下来的9位产生9位无符号整数。

I claim there are 4 possible outcomes: 我声称有4种可能的结果:

byteOrder,    bitOrder

* bigEndian,    bigEndian    results in hex 0BA or decimal 186
* littleEndian, littleEndian results in hex 1BD or decimal 445
* littleEndian, bigEndian    results in hex 05D or decimal 93
* bigEndian,    littleEndian results in hex 1DE or decimal 478

I have seen the first 3 of these 4 in data. 我已经在数据中看到了这4个中的前3个。 big, big, and little, little are easy to work out. 大,小,小,很容易锻炼。

Hint for dealing with this. 提示处理这个问题。

If the byte order is big endian, write down bytes from left increasing to right. 如果字节顺序是大端,则从左向右写入字节。 If the byte order is little endian, write down bytes from right increasing to left. 如果字节顺序是小端,则从右向左写入字节。

A bit confusing :-). 有点混乱:-)。 Except in serial communication, the term "first bit" has no meaning, there are only left-most (most significant) and right-most (least significant) bits. 除串行通信外,术语“第一位”没有意义,只有最左侧(最高位)和最右侧(最低位)位。 If someone told you to extract "the first three bits" then give them a slap and ask what they meant. 如果有人告诉你要提取“前三位”然后给他们一个耳光并问他们的意思。 Even the term "Bit 0" is ambiguous, it often means the least significant, right-most bit (the 2**0 bit) but is almost as often used to mean the most significant, left-most bit in some bit-field. 即使术语“位0”是模糊的,它通常意味着最低有效位,最右位(2 ** 0位),但几乎常用于表示某些位域中最重要的最左位。 Which bit is the "first" bit in a byte depends entirely on what you are doing with the bits. 哪个位是字节中的“第一”位完全取决于您对这些位的处理。

The bitwise operators in C are defined to work on values . C中的按位运算符被定义为处理 The expression 0x41U >> 5 will always give the value 2 (in binary, 010). 表达式0x41U >> 5将始终给出值2(二进制,010)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM