Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Details about Endian-ness and .Net?

Tags:

c#

endianness

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

like image 755
Corey Ogburn Avatar asked Jul 02 '10 02:07

Corey Ogburn


People also ask

What is endianness used for?

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.

What is endian system?

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.

What is little-endian and big-endian with example?

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

What is difference between big-endian and little-endian?

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 Answers

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.

like image 123
Stephen Cleary Avatar answered Oct 07 '22 12:10

Stephen Cleary