|

楼主 |
发表于 2019-6-19 13:58:57
|
显示全部楼层
- /* 方法1: 直接逐一交换bit*/
- const uint32_t arr[] = { 0x80000000,0x40000000,0x20000000,0x10000000,
- 0x08000000,0x04000000,0x02000000,0x01000000,
- 0x00800000,0x00400000,0x00200000,0x00100000,
- 0x00080000,0x00040000,0x00020000,0x00010000,
- 0x00008000,0x00004000,0x00002000,0x00001000,
- 0x00000800,0x00000400,0x00000200,0x00000100,
- 0x00000080,0x00000040,0x00000020,0x00000010,
- 0x00000008,0x00000004,0x00000002,0x00000001
- };
- uint32_t reverseBits(uint32_t n)
- {
- uint32_t i = 0;
- uint32_t bit_head = 0;
- uint32_t bit_tail = 0;
-
- for(i = 0; i < 16 ; i++)
- {
- bit_head = n & ( arr[i] );
- bit_tail = n & ( arr[31-i] );
-
- if(!bit_head == !bit_tail)
- {
- continue;
- }
-
- if(bit_head)
- {
- n |= arr[31-i];
- }
- else
- {
- n &= ~arr[31-i];
- }
- if(bit_tail)
- {
- n |= arr[i];
- }
- else
- {
- n &= ~arr[i];
- }
- }
- return n;
- }
复制代码
- /*
- 方法1:
- 执行用时 : 0 ms , 在所有 C 提交中击败了 100.00% 的用户
- 内存消耗 : 6.8 MB , 在所有 C 提交中击败了 6.38% 的用户
- */
复制代码 |
|