On Thanksgiving, we often rearrange and reflect on what we have, sometimes mixing things up to see them from a new perspective. In this problem, you'll perform a similar operation on the bits of a 32-bit unsigned integer.
Write a function that takes in a 32-bit unsigned integer n
and an integer k
(where k
evenly divides 32). Your task is to divide the 32-bit binary representation of n
into 32/k
groups, each containing k
bits, reverse the bits within each group, and then return the resulting integer.
n
will be represented in 32 bits.k
is guaranteed to be a divisor of 32 (e.g., 1, 2, 4, 8, 16, 32).For example, let n = 43261596
which is represented in binary as:
00000010 10010100 00011110 10011100
If k = 8
, then the groups are:
00000010 10010100 00011110 10011100
Reversing each group individually gives:
01000000 00101001 01111000 00111001
Which corresponds to the new integer (in decimal). (Note: The actual output value can be computed by converting the final binary back to a decimal number.)
Your function should accept two parameters: n
(an unsigned 32-bit integer) and k
(an integer divisor of 32) and return the resulting unsigned 32-bit integer after processing.
Below is some starter code in multiple languages to help you begin your solution.