Is there a built-in function to reverse bit order

后端 未结 8 1371
情书的邮戳
情书的邮戳 2020-12-13 00:34

I\'ve come up with several manual ways of doing this, but i keep wondering if there is something built-in .NET that does this.

Basically, i want to reverse the bit o

8条回答
  •  孤街浪徒
    2020-12-13 01:02

    private UInt32 BitReverse(UInt32 value)
    {
      UInt32 left = (UInt32)1 << 31;
      UInt32 right = 1;
      UInt32 result = 0;
    
      for (int i = 31; i >= 1; i -= 2)
      {
        result |= (value & left) >> i;
        result |= (value & right) << i;
        left >>= 1;
        right <<= 1;
      }
      return result;
    }
    

提交回复
热议问题