`
standalone
  • 浏览: 595811 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

how to reverse bits in a byte?

阅读更多
Given a 8-bit byte, assume its bits are b8b7...b1.
Provide an algorithm to reverse the bit sequence.
Result should be b1b2b3...b8.

An simple answer:

Suppose the byte is c.

c=((c>>1) & 0x55) | ((c<<1) & 0xAA));  // we got 78563412

c=((c>>2) & 0x33) | ((c<<2) & 0xCC));  // we got 56781234

c=((c>>4) | (c<<4)); // we got 12345678
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics