Why does `OpCode.Value` have the "wrong" endianness?

Viewed 564

Facts:

  1. The correct encoding for the CIL instruction rethrow's op-code is the two-byte sequence FE 1A.

  2. OpCodes.Rethrow.Value (which has type short) has value 0xFE1A on my little-endian machine.

  3. BitConverter honours the machine's endianness when converting to/from byte sequences.

  4. On my little-endian machine, BitConverter.GetBytes(OpCodes.Rethrow.Value) results in the byte sequence 1A FE.

That means, serializing an OpCode.Value on a little-endian machine using BitConverter does not produce the correct encoding for the op-code; the byte order is reversed.

Questions:

  • Is the byte ordering of OpCode.Value documented (and if so, where?), or is it an "implementation detail"?

  • Does step 4 above on a big-endian machine also result in the wrong byte ordering? That is, would OpCodes.Rethrow.Value be 0x1AFE on a big-endian machine?

3 Answers
Related