Reverse bit order


/ Published in: C#
Save to your folder(s)

Reverse bit order of a byte variable, e.g. 0000 0001 (0x01) --> 1000 000 (0x80).


Copy this code and paste it in your HTML
  1. //Add as static field of a Class
  2. static byte[] _RevBitsTable =
  3. {
  4. 0x00,0x08,0x04,0x0C,
  5. 0x02,0x0A,0x06,0x0E,
  6. 0x01,0x09,0x05,0x0D,
  7. 0x03,0x0B,0x07,0x0F
  8. };
  9.  
  10. //Add as method of a Class
  11. static byte ReverseBits(byte data)
  12. {
  13. byte ln = _RevBitsTable[data & 0x0F];
  14. byte hn = _RevBitsTable[(data >> 4) & 0x0F];
  15. return (byte)((ln << 4) | hn);
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.