I am reading a file byte-by-byte.
Say for example i have this byte: 0x41 (0100 0001) represented in hex.
Now, I want the first three bits of this byte, i.e (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.(i.e they can't be 001)?
Thanks,
Bit order usually follows the same endianness as the byte order for a given computer system. That is, in a big endian system the most significant bit is stored at the lowest bit address; in a little endian system, the least significant bit is stored at the lowest bit address.
Note that within both big-endian and little-endian byte orders, the bits within each byte are big-endian. This means the bit stream represented by a given number of stored bytes doesn't attempt to be big- or little-endian.
Endian and endianness (or "byte-order") describe how computers organize the bytes that make up numbers. Each memory storage location has an index or address. Every byte can store an 8-bit number (i.e. between 0x00 and 0xff ), so you must reserve more than one byte to store a larger number.
Little Endian byte ordering is defined as follows: In a binary number consisting of multiple bytes (e.g., a 32-bit unsigned integer value, the Group Number, the Element Number, etc.), the least significant byte shall be encoded first; with the remaining bytes encoded in increasing order of significance.
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.
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:
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). However, C bit fields really have nothing to do with hardware addressing.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With