With the help of various sources, I have written some SwapBytes
methods in my binary reader class that swap endian in ushort
, uint
, an
This is probably the easiest and lazy way to replace bits in an integer:
using System;
namespace BitSwap
{
class Program
{
static void Main()
{
//swaps bits {p, p+1, …, p+k-1} with bits {q, q+1, …, q+k-1} of n.
Console.WriteLine("n=");
uint n = uint.Parse(Console.ReadLine());
Console.WriteLine("p=");
int p = int.Parse(Console.ReadLine());
Console.WriteLine("q=");
int q = int.Parse(Console.ReadLine());
Console.WriteLine("k=");
int k = int.Parse(Console.ReadLine());
int i;
int s;
if ((p + k - 1) < 32 && (q + k - 1) < 32 && p > 0 && q > 0)
// for integer
{
for (i = p, s = q; i <= p + k - 1 && s <= q + k - 1; i++, s++)
{
uint firstBits = (n >> i) & 1;
uint secondBits = (n >> s) & 1;
uint maskFirstBits = (uint)1 << i;
uint maskSecondBits = (uint)1 << s;
n = (n & ~maskFirstBits) | (secondBits << i);
n = (n & ~maskSecondBits) | (firstBits << s);
}
Console.WriteLine("Result: {0}", n);
}
else
{
Console.WriteLine("Invalid entry.");
}
}
}
}
Just add a cast to unsigned at the start and back to signed at the end.
public long SwapBytes(long value)
{
return (long)SwapBytes((ulong)value);
}
It might be necessary to manually inline the call to SwapBytes
for maximal performance.
On a different note, you might want to avoid swapping, in favour of directly reading the data from the original byte array in the desired endianness. See Efficient way to read big endian data in C# for details.
Instead of conceptually deconstructing to separate bytes and then reassembling them the other way around, you can conceptually swap groups of bytes, like this: (not tested)
public uint SwapBytes(uint x)
{
// swap adjacent 16-bit blocks
x = (x >> 16) | (x << 16);
// swap adjacent 8-bit blocks
return ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
}
Doesn't help much (or at all) for 32 bits, but for 64 bits it does (not tested)
public ulong SwapBytes(ulong x)
{
// swap adjacent 32-bit blocks
x = (x >> 32) | (x << 32);
// swap adjacent 16-bit blocks
x = ((x & 0xFFFF0000FFFF0000) >> 16) | ((x & 0x0000FFFF0000FFFF) << 16);
// swap adjacent 8-bit blocks
return ((x & 0xFF00FF00FF00FF00) >> 8) | ((x & 0x00FF00FF00FF00FF) << 8);
}
For signed types, just cast to unsigned, do this, then cast back.
You should have a look to following msdn page : http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx
You may simply use Array.Reverse and bitConverter:
int value = 12345678;
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
int result = BitConverter.ToInt32(bytes, 0);