Turn two bytes into a UInt16 and back again?


I'm working on a simple emulator project (for debugging, not gaming), so I have a class representing a 16-bit structure with LowerByte and UpperByte fields, both bytes.

Actual Question

I have four bytes in pairs, and I need to perform operations on them (add, subtract, etc.) as two 16-bit unsigned integers. The simplest way would probably be to combine the byte pairs into UInt16s and perform my operations on those.

What's the best way to turn two bytes into a single UInt16?

I had considered converting both bytes into UInt16s, and rotating the upper byte's UInt16 left 8 bits, and adding the result. It works, but C# usually has some amazingly simple method for doing things like this built in.

Any suggestions?

by cadika_orade via /r/csharp

Leave a Reply