Grouped Bit Reversal

Bit Manipulation Bitwise Coding

Problem: Grouped Bit Reversal

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.

Description

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.

Details

  • The 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).
  • You should not alter the order of the groups themselves; only reverse the bits within each group.

Example

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.)

Function Signature

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.

Starter Code

Below is some starter code in multiple languages to help you begin your solution.