Main Site Documentation

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


#1

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


#2

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.


#3

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;
        }


#4

Thanks, I updated the code to use your technique.