-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
public class Solution { | ||
/* T.C = O(1) | ||
* S.C = O(1) | ||
*/ | ||
public int reverseBits(int n) { | ||
// Set the output to 0 | ||
int output = 0; | ||
// Repeat 32 times | ||
for (int i = 0; i < 32; i++) { | ||
// Shift the output value one space to the left to make room for the new bit | ||
output <<= 1; | ||
// '&' operation to get the rightmost bit and add it to the output | ||
output = output | (n & 1); | ||
// Discard the rightmost bit of the 'n' | ||
n = n >> 1; | ||
} | ||
|
||
return output; | ||
|
||
} | ||
} | ||
|