1367. Bit Reversal of 32-bit Integer

MediumBit ManipulationBit ManipulationReversalMasking

Treat n as a 32-bit unsigned integer and split it into 32/k consecutive groups of k bits, where k divides 32. Reverse the order of those groups and return the resulting 32-bit value. With k = 1 every individual bit is reversed, which is the classic 32-bit bit-reversal; with k = 8 the four bytes are reversed, which is a byte-order (endianness) swap.

Input: A JSON object {"n": <32-bit unsigned integer>, "k": <group size>} where k is one of 1, 2, 4, 8, 16, 32.

Output: Return the 32-bit value with its k-bit groups in reverse order.

Examples

Example 1
Input: {"n":43261596,"k":1}
Output: 964176192
Explanation: Reversing all 32 bits of 43261596 gives 964176192.
Example 2
Input: {"n":305419896,"k":8}
Output: 2018915346
Explanation: The bytes of 0x12345678 reverse to 0x78563412.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →