c++ - reverse a number's bits -
here c++ class revering bits leetcode discuss. https://leetcode.com/discuss/29324/c-solution-9ms-without-loop-without-calculation example, given input 43261596 (represented in binary 00000010100101000001111010011100), return 964176192 (represented in binary 00111001011110000010100101000000).
is there can explain it? thank much!!
class solution { public: uint32_t reversebits(uint32_t n) { struct bs { unsigned int _00:1; unsigned int _01:1; unsigned int _02:1; unsigned int _03:1; unsigned int _04:1; unsigned int _05:1; unsigned int _06:1; unsigned int _07:1; unsigned int _08:1; unsigned int _09:1; unsigned int _10:1; unsigned int _11:1; unsigned int _12:1; unsigned int _13:1; unsigned int _14:1; unsigned int _15:1; unsigned int _16:1; unsigned int _17:1; unsigned int _18:1; unsigned int _19:1; unsigned int _20:1; unsigned int _21:1; unsigned int _22:1; unsigned int _23:1; unsigned int _24:1; unsigned int _25:1; unsigned int _26:1; unsigned int _27:1; unsigned int _28:1; unsigned int _29:1; unsigned int _30:1; unsigned int _31:1; } *b = (bs*)&n, c = { b->_31, b->_30, b->_29, b->_28 , b->_27, b->_26, b->_25, b->_24 , b->_23, b->_22, b->_21, b->_20 , b->_19, b->_18, b->_17, b->_16 , b->_15, b->_14, b->_13, b->_12 , b->_11, b->_10, b->_09, b->_08 , b->_07, b->_06, b->_05, b->_04 , b->_03, b->_02, b->_01, b->_00 }; return *(unsigned int *)&c; } };
consider casting providing different layout stencil on memory.
using stencil picture, code layout of stencil of 32-bits on unsigned integer memory location.
so instead of treating memory uint32_t
, treating memory 32 bits.
a pointer 32-bit structure created.
the pointer assigned same memory location uint32_t
variable.
the pointer allow different treatment of memory location.
a temporary variable, of 32-bits (using structure), created. variable initialized using initialization list.
bit fields in initialization list original variable, listed in reverse order.
so, in list:
new bit 0 <-- old bit 31 new bit 1 <-- old bit 30
the foundation of approach relies on initialization lists. author letting compiler reverse bits.
Comments
Post a Comment