tags:

views:

108

answers:

2

I have a few questions about endian-ness that are related enough that I warrant putting them in as one question:

1) Is endian-ness decided by .Net or by the hardware?

2) If it's decided by the hardware, how can I figure out what endian the hardware is in C#?

3) Does endian-ness affect binary interactions such as ORs, ANDs, XORs, or shifts? I.E. Will shifting once to the right always shift off the least significant bit?

4) I doubt it, but is there a difference in endian-ness from different versions of the .Net framework? I assume they're all the same, but I've learned to stop assuming about some of the lower level details such as this.

If need be, I can ask these as different questions, but I figure anybody who knows the answer to one of these probably knows the answer to all of them (or can point me in a good direction).

+10  A: 

1) The hardware.

2) BitConverter.IsLittleEndian

3) Endianness does not affect bitwise operations. Shifting to the right is shifting in the least-significant-bit direction. UPDATE from Oops' comment: However, endianness does affect binary input and output. When reading or writing values larger than a byte (e.g., reading an int from a BinaryReader or using BitConverter), you do have to account for endianness. Once the values are read in correctly, then all bitwise operations act as normal.

4) Most versions of .NET are little endian. Notable exceptions include the XBox and some platforms supported by Mono or the Compact Framework.

Stephen Cleary
I'm not sure #4 is entirely correct. For example, the .Net Compact Framework runs on many devices, I'd assume some of them are big-endian. And Mono (although not .Net, but a valid CLR implementation) certainly runs on big-endian platforms. I think Corey was asking whether the endianness differs with incremental versions of .Net (eg, 1.0 .. 4.0) - the answer to that is no - it depends on hardware only.
Mark H
I'll second @Mark H comment. The endianness is decided by the hardware and .Net will just use whatever it is. However, no classes in .Net will rely on particular endianness (or at least no classes shouldn't :-)), so at best you can say it's agnostic, not that it has particular endianness.
Franci Penov
@Mark: I've updated the answer to reflect Mono/CF. I believe the only BE CF platform is XBox.
Stephen Cleary
I am a bit wondering about your answer to 3. Of course endiannes does affect bit shifting. If you read a integer and do not interpret it to the right endiannes, bit shifting would destroy it.
Oops
@Oops: If you read an integer with the wrong endianness then it'll be wrong anyway (for example, 0x12345678 might be read as 0x78563412). So long as the data has been read correctly in the first place then bitwise ops should be fine.
LukeH
@LukeH thanks for the explanation, that's what I said already. It was just missing in Stephens otherwise very good answer.
Oops
A: 

1) neither nor... or you can say either
it was decide by the hardware developers. And you must decide about it if you write software that read/writes certain file formats without using external libraries.
There is no problem with endiannes If you read from a file
* a byte
but you have to decide how to interprete it in little or big endian format if you read all other primitive data types like
* integers,
* strings,
* floats.
The hardware does not help here. The BitConverter does not help here. Only the documentation of the file format could help and testing your code of course...
edit: found ad good explanation here: http://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/

Oops