Bit ordering and consistency

I am reading a file byte by byte.

Say, for example, I have this byte: 0x41 (0100 0001), represented in hexadecimal format.

Now I want the first three bits of this byte, that is (010).

I can use bitwise logic to extract the first three bits, but my question is, will the first three bits be independent of the enzyme of the machine (i.e. they cannot be 001)?

Thanks,

+6
c bit-manipulation endianness
source share
6 answers

Another way to think about this is that endianness only applies when you can read the components of an element individually - since you can usually read individual bytes of a 32-bit int from memory if you want to interpret these bytes as a 32-bit int to you need to make sure that the enthusiasm of architecture is taken into account.

Usually, you cannot read individual bits of a byte from memory, so there really is no concept of โ€œbit-essenceโ€ in terms of memory architecture (I am sure there is a hardware level, but not something that you can see at the software level ) In several areas where you may need to (or at least know) a bit entity:

  • the order in which the compiler stores the bit of the bit field depends on the compiler (and is not necessarily related to the end of the hardware platform), different compilers can arrange the bit fields differently for the same platform - it is possible that the compiler can be configured one way or another using command line options, similar to how char can be set as signed or unsigned). However, C bit fields really have nothing to do with hardware addressing.

  • Some hardware architectures allow you to address individual bits (for example, ARM Cortex M3), so in this case you will need to know how the architecture intended for bits should be addressed if you use this feature.

  • if you send bits via a serial link, the hardware interface will usually indicate whether the most significant bit or the least significant bit is first shifted to the wire.

+7
source share

Concreteness applies only to byte order, not to bit order. The order of the bits will be the same in the corresponding byte.

+7
source share

Yes, they will be the same.

Bit ordering inside bytes, as a rule, is only a problem when you perform bitwise I / O, for example, when reading a stream of data transmitted over a serial line. They really send one bit at a time, so the sender and receiver need to agree if the bit is sent from left to right or from right to left for each byte.

For files and memory accesses in bits, the byte order is not changed.

+3
source share

A bit confusing :-). With the exception of serial communication, the term "first bit" does not make sense, there are only the leftmost (most significant) and rightmost (least significant) bits. If someone told you to extract the "first three bits", give them a slap in the face and ask what they meant. Even the term โ€œbit 0โ€ is ambiguous, it often means the least significant, rightmost bit (bit 2 ** 0 bits), but is almost as often used to denote the most significant, left-most bit in some bit field, Which bit is the โ€œfirst "a bit in a byte depends entirely on what you do with the bits.

+1
source share

Bitwise operators in C are defined for working with values. An expression 0x41U >> 5 will always give the value 2 (in binary format, 010).

+1
source share

The order of the bits matters when the field uses a part of the byte or spans bytes beginning or ending (or both) part of the path through the byte.

Example: 2 bytes of data, first 235 (decimal), second 173 (decimal), as well as hexadecimal EB and AD.

I want the bit to start with the fourth bit through the 12th bit. So, skip over 3 bits, make a 9-bit unsigned integer from the next 9 bits.

I argue that there are 4 possible outcomes:

  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 saw the first 3 of these 4 in the data. big, big and small, little can be done.

Hint for solving this problem.

If the byte order is large endian, write the bytes from top left to right. If the byte order is slightly narrowed, write the bytes on the right, increasing to the left.

0
source share

All Articles