Snippet - How to get/set/reverse or invert bits in a byte

I just posted How to get/set/reverse or invert bits in a byte. on Codeshare. Feel free to discuss and make suggestions here.

I know this is a pathetically obvious snibbit, but I had to Google for this code a few months ago when I got started with NETMF.

You can reverse bits quite efficiently (speed wise) using interchange scheme:


        public static byte ReverseByte(byte x)
        {
            x = (byte)((x & 0x55) << 1 | (x & 0xAA) >> 1);
            x = (byte)((x & 0x33) << 2 | (x & 0xCC) >> 2);
            x = (byte)((x & 0x0F) << 4 | (x & 0xF0) >> 4);
            return x;
        }

1 Like

Thanks, I updated the code to use your technique.