|
| 1 | +--- |
| 2 | +id: Reverse-Bits |
| 3 | +title: Reverse-Bits |
| 4 | +sidebar_label: Reverse Bits |
| 5 | +tags: |
| 6 | + - Bit Manipulation |
| 7 | + - Integer Manipulation |
| 8 | +--- |
| 9 | + |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 13 | +| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ | |
| 14 | +| [Reverse-Bits](https://leetcode.com/problems/Reverse-Bits/description/) | [Reverse-Bits Solution on LeetCode](https://leetcode.com/problems/Reverse-Bits/solutions/) | [Nikita Saini](https://leetcode.com/u/Saini_Nikita/) | |
| 15 | + |
| 16 | + |
| 17 | +## Problem Description |
| 18 | + |
| 19 | +Reverse the bits of a given 32-bit unsigned integer. |
| 20 | + |
| 21 | +### Note |
| 22 | + |
| 23 | +In some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. |
| 24 | + |
| 25 | +In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 below, the input represents the signed integer -3 and the output represents the signed integer -1073741825. |
| 26 | + |
| 27 | +### Examples |
| 28 | + |
| 29 | +**Example 1:** |
| 30 | + |
| 31 | +Input: `n = 00000010100101000001111010011100` |
| 32 | +Output: `964176192` (Binary: `00111001011110000010100101000000`) |
| 33 | +Explanation: The input binary string `00000010100101000001111010011100` represents the unsigned integer `43261596`, so return `964176192` which its binary representation is `00111001011110000010100101000000`. |
| 34 | + |
| 35 | +**Example 2:** |
| 36 | + |
| 37 | +Input: `n = 11111111111111111111111111111101` |
| 38 | +Output: `3221225471` (Binary: `10111111111111111111111111111111`) |
| 39 | +Explanation: The input binary string `11111111111111111111111111111101` represents the unsigned integer `4294967293`, so return `3221225471` which its binary representation is `10111111111111111111111111111111`. |
| 40 | + |
| 41 | +### Constraints |
| 42 | + |
| 43 | +- The input must be a binary string of length 32. |
| 44 | + |
| 45 | +## Approach |
| 46 | + |
| 47 | +To reverse the bits of a 32-bit unsigned integer, we can use bit manipulation. We will: |
| 48 | + |
| 49 | +1. Initialize a variable to hold the result. |
| 50 | +2. Iterate through each bit of the input number. |
| 51 | +3. For each bit, shift the result left by one position and add the current bit. |
| 52 | +4. Shift the input number right by one position. |
| 53 | +5. Continue until all 32 bits are processed. |
| 54 | + |
| 55 | +## Solution |
| 56 | + |
| 57 | +### Step-by-Step Algorithm |
| 58 | + |
| 59 | +1. Initialize the result to 0. |
| 60 | +2. For each of the 32 bits: |
| 61 | + 1. Left shift the result by 1 bit. |
| 62 | + 2. Add the least significant bit (LSB) of `n` to the result. |
| 63 | + 3. Right shift `n` by 1 bit. |
| 64 | +3. Return the result. |
| 65 | + |
| 66 | +### Python |
| 67 | + |
| 68 | +```python |
| 69 | +class Solution: |
| 70 | + def reverseBits(self, n: int) -> int: |
| 71 | + result = 0 |
| 72 | + for i in range(32): |
| 73 | + result = (result << 1) | (n & 1) |
| 74 | + n >>= 1 |
| 75 | + return result |
| 76 | +``` |
| 77 | + |
| 78 | +### Java |
| 79 | +```java |
| 80 | +public class Solution { |
| 81 | + public int reverseBits(int n) { |
| 82 | + int result = 0; |
| 83 | + for (int i = 0; i < 32; i++) { |
| 84 | + result = (result << 1) | (n & 1); |
| 85 | + n >>= 1; |
| 86 | + } |
| 87 | + return result; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### C++ |
| 93 | +```cpp |
| 94 | +class Solution { |
| 95 | +public: |
| 96 | + uint32_t reverseBits(uint32_t n) { |
| 97 | + uint32_t result = 0; |
| 98 | + for (int i = 0; i < 32; i++) { |
| 99 | + result = (result << 1) | (n & 1); |
| 100 | + n >>= 1; |
| 101 | + } |
| 102 | + return result; |
| 103 | + } |
| 104 | +}; |
| 105 | +``` |
| 106 | +
|
| 107 | +### C |
| 108 | +```c |
| 109 | +uint32_t reverseBits(uint32_t n) { |
| 110 | + uint32_t result = 0; |
| 111 | + for (int i = 0; i < 32; i++) { |
| 112 | + result = (result << 1) | (n & 1); |
| 113 | + n >>= 1; |
| 114 | + } |
| 115 | + return result; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### JavaScript |
| 120 | +```js |
| 121 | +var reverseBits = function(n) { |
| 122 | + let result = 0; |
| 123 | + for (let i = 0; i < 32; i++) { |
| 124 | + result = (result << 1) | (n & 1); |
| 125 | + n >>= 1; |
| 126 | + } |
| 127 | + return result >>> 0; |
| 128 | +}; |
| 129 | +``` |
| 130 | + |
| 131 | +### Conclusion |
| 132 | +The bit manipulation technique efficiently reverses the bits of a 32-bit unsigned integer. The key steps involve shifting and masking bits appropriately within a loop that iterates exactly 32 times. This approach ensures that the solution is both time-efficient and space-efficient, with a time complexity of O(1) and a space complexity of O(1). |
0 commit comments