Skip to content

Instantly share code, notes, and snippets.

@dnedrow
Last active September 21, 2020 00:20
Show Gist options
  • Select an option

  • Save dnedrow/b891556414a0a06fd2d4b3bf66c12038 to your computer and use it in GitHub Desktop.

Select an option

Save dnedrow/b891556414a0a06fd2d4b3bf66c12038 to your computer and use it in GitHub Desktop.
C# extensions for converting between UInt32 and UInt16
/// <summary>
/// Joins two 16bit integers to a single 32bit value.
/// </summary>
/// <param name="int1">The integer to use for the high bits.</param>
/// <param name="int2">The integer to use for the low bits.</param>
/// <returns>An unsigned 32bit integer.</returns>
public static UInt32 Join(this UInt16 int1, UInt16 int2)
{
return (uint) ((int1 << 16) | int2);
}
/// <summary>
/// Splits a single 32bit integer to two unsigned 16bit integers.
/// </summary>
/// <param name="int1">The 32bit integer to be split.</param>
/// <returns>Two unsigned 16bit integers. The high bits are placed
/// in Item1, the lower in Item2.</returns>
public static (UInt16, UInt16) Split(this UInt32 int1)
{
var high = System.Convert.ToUInt16((int1 >> 16) & 0xffff);
var low = System.Convert.ToUInt16(int1 & 0xffff);
return (high, low);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment