views:

220

answers:

5

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,

+6  A: 

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

sharptooth
+3  A: 

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. 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.

unwind
+5  A: 

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:

  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). However, C bit fields really have nothing to do with hardware addressing.

  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.

  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.

Michael Burr
+1  A: 

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. Which bit is the "first" bit in a byte depends entirely on what you are doing with the bits.

Denis Howe
+1  A: 

The bitwise operators in C are defined to work on values. The expression 0x41U >> 5 will always give the value 2 (in binary, 010).

caf