Using various sources, I wrote several SwapBytes methods in my class of binary readers that change the final element in ushort , uint and ulong , all use bitwise operations in the original C #, without any unsafe code needed.
public ushort SwapBytes(ushort x) { return (ushort)((ushort)((x & 0xff) << 8) | ((x >> 8) & 0xff)); } public uint SwapBytes(uint x) { return ((x & 0x000000ff) << 24) + ((x & 0x0000ff00) << 8) + ((x & 0x00ff0000) >> 8) + ((x & 0xff000000) >> 24); } public ulong SwapBytes(ulong value) { ulong uvalue = value; ulong swapped = ((0x00000000000000FF) & (uvalue >> 56) | (0x000000000000FF00) & (uvalue >> 40) | (0x0000000000FF0000) & (uvalue >> 24) | (0x00000000FF000000) & (uvalue >> 8) | (0x000000FF00000000) & (uvalue << 8) | (0x0000FF0000000000) & (uvalue << 24) | (0x00FF000000000000) & (uvalue << 40) | (0xFF00000000000000) & (uvalue << 56)); return swapped; }
How can I start creating the same methods, but for signed versions of each of these types, for example short, int and long, using only the same methods as above, and what improvements can be made to the methods above?
source share