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).
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.
Endianness is a term that describes the order in which a sequence of bytes is stored in computer memory. Endianness can be either big or small, with the adjectives referring to which value is stored first.
Specifically, little-endian is when the least significant bytes are stored before the more significant bytes, and big-endian is when the most significant bytes are stored before the less significant bytes. When we write a number (in hex), i.e. 0x12345678 , we write it with the most significant byte first (the 12 part).
A big-endian system stores the most significant byte of a word at the smallest memory address and the least significant byte at the largest. A little-endian system, in contrast, stores the least-significant byte at the smallest address.
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.
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