-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
190.c
26 lines (26 loc) · 1.14 KB
/
190.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
uint32_t reverseBits(uint32_t n)
{
uint TotalBits = 32;
uint32_t reverse_int = 0; // stored in memory as 32 bits, each bit valued 0
uint i;
for (i = 0; i < TotalBits; i++)
{
if ((n & (UINT32_C(1)
<< 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
reverse_int =
reverse_int |
(UINT32_C(1)
<< (TotalBits - 1 -
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)
}
return reverse_int;
}