2020-05-29 20:23:24 +00:00
|
|
|
uint32_t reverseBits(uint32_t n)
|
|
|
|
{
|
2019-10-04 01:11:00 +05:30
|
|
|
uint TotalBits = 32;
|
2020-06-28 15:25:37 +00:00
|
|
|
uint32_t reverse_int = 0; // stored in memory as 32 bits, each bit valued 0
|
2019-10-04 01:11:00 +05:30
|
|
|
uint i;
|
2020-05-29 20:23:24 +00:00
|
|
|
for (i = 0; i < TotalBits; i++)
|
|
|
|
{
|
2020-05-29 20:25:52 +00:00
|
|
|
if ((n & (UINT32_C(1)
|
2020-06-28 15:25:37 +00:00
|
|
|
<< i))) // if the bit on the ith position of 32 bit input is
|
|
|
|
// 1, then proceed Further note the use of UINT32_C
|
|
|
|
// to convert 1 to unsigned 32 bit int, since just 1
|
|
|
|
// is treated as int which cannot be shifted left
|
|
|
|
// more than 30 times
|
2020-05-29 20:23:24 +00:00
|
|
|
reverse_int =
|
|
|
|
reverse_int |
|
|
|
|
(UINT32_C(1)
|
|
|
|
<< (TotalBits - 1 -
|
2020-06-28 15:25:37 +00:00
|
|
|
i)); // Convert the ith bit from the end in reverse_int
|
|
|
|
// from 0 to 1, if ith bit from beginning in n is 1
|
|
|
|
// This is achieved by using bitwise OR on
|
|
|
|
// reverse_int (where ith bit from end is currently
|
|
|
|
// 0) and 1 shifted left 31 - i bits (to ith bit from
|
|
|
|
// the end)
|
2019-10-04 01:11:00 +05:30
|
|
|
}
|
|
|
|
return reverse_int;
|
|
|
|
}
|