Skip to content

Instantly share code, notes, and snippets.

@ForeverZer0
Created August 24, 2021 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ForeverZer0/3c8e09fef1da5634e9d573d3c6b52f49 to your computer and use it in GitHub Desktop.
Save ForeverZer0/3c8e09fef1da5634e9d573d3c6b52f49 to your computer and use it in GitHub Desktop.
Swap endian of numeric types using grouped bit-shifts instead of the much slower Array.Reverse.
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
namespace ChangeThisNamespace
{
/// <summary>
/// Contains extension methods dealing with endianness of numeric types.
/// </summary>
[PublicAPI]
public static class EndianExtensions
{
/// <summary>
/// Swap the endian of the given <paramref name="value"/>.
/// </summary>
/// <param name="value">The value to swap endian of.</param>
/// <returns>The value with bytes in opposite format.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short SwapEndian(this short value)
{
return (short) ((value << 8) | ((value >> 8) & 0xFF));
}
/// <inheritdoc cref="SwapEndian(short)"/>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort SwapEndian(this ushort value)
{
return (ushort)((value << 8) | (value >> 8 ));
}
/// <inheritdoc cref="SwapEndian(short)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SwapEndian(this int value) => unchecked((int) SwapEndian(unchecked((uint)value)));
/// <inheritdoc cref="SwapEndian(short)"/>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint SwapEndian(this uint value)
{
value = ((value << 8) & 0xFF00FF00 ) | ((value >> 8) & 0xFF00FF );
return (value << 16) | (value >> 16);
}
/// <inheritdoc cref="SwapEndian(short)"/>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong SwapEndian(this ulong val)
{
val = ((val << 8) & 0xFF00FF00FF00FF00UL ) | ((val >> 8) & 0x00FF00FF00FF00FFUL );
val = ((val << 16) & 0xFFFF0000FFFF0000UL ) | ((val >> 16) & 0x0000FFFF0000FFFFUL );
return (val << 32) | (val >> 32);
}
/// <inheritdoc cref="SwapEndian(short)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long SwapEndian(this long value) => unchecked((long) SwapEndian(unchecked((ulong)value)));
/// <inheritdoc cref="SwapEndian(short)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float SwapEndian(this float value)
{
var n = BitConverter.SingleToInt32Bits(value);
return BitConverter.Int32BitsToSingle(n.SwapEndian());
}
/// <inheritdoc cref="SwapEndian(short)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double SwapEndian(this double value)
{
var n = BitConverter.DoubleToInt64Bits(value);
return BitConverter.Int64BitsToDouble(n.SwapEndian());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment