Bit reversing in C#, Part 2

So further to my exposition of Part 1 of the series, I’d like to further elucidate the reason why I think this is a worthwhile pursuit;

1.   When was the last time anyone asked you to do bitshifting in .NET?

2.   This is one of the microproblems. The metrics involved are so precise, it will improve your algorithmic capabilities just thinking about it.

3.    Boredom.

So right then. the next solution proposed is a lookup table. The nice thing about this, if you think about it, is that there is a very limited series of inputs and therefore outputs. so,  using the previous post as a starting point;


private static byte[] byteTable = new byte[256];

for (int a = 0; a < 256; a++)
{
    byteTable[a] = swapBits((byte) a);
}

private static byte reverseByte(byte inByte)
{
    return byteTable[inByte];
}

And hey presto. We have it in one cycle (after the initial startup) but is there a way to get around having to do this? More in Part 3…

Advertisements

About this entry