4
4
* 190. Reverse Bits
5
5
* Reverse bits of a given 32 bits unsigned integer.
6
6
7
- For example, given input 43261596 (represented in binary as 00000010100101000001111010011100),
8
- return 964176192 (represented in binary as 00111001011110000010100101000000).
7
+ Example:
8
+ Input: 43261596
9
+ Output: 964176192
9
10
10
- Follow up:
11
- If this function is called many times, how would you optimize it?
11
+ Explanation: 43261596 represented in binary as 00000010100101000001111010011100,
12
+ return 964176192 represented in binary as 00111001011110000010100101000000.
13
+
14
+ Follow up:
15
+ If this function is called many times, how would you optimize it?
12
16
*/
13
17
14
18
public class _190 {
@@ -23,47 +27,24 @@ public class _190 {
23
27
* gives a good explanation between logical right shift: ">>>" and arithmetic right shift: ">>".
24
28
* Basically, ">>" preserves the most left bit and treats it as the sign for this number,
25
29
* e.g. -2 represented in 8 bits is 11111110, thus -2 >> 1 will become 11111111, i.e. -1
26
- * notice its sign bit (the most left one bit) is preserved
27
- * However, logical right shift ">>>" doesn't care about the first bit on the most left,
30
+ * notice its sign bit (the most left one bit) is preserved
31
+ * However, logical right shift ">>>" doesn't care about the first bit on the most left,
28
32
* it simply shifts every bit to the right.
29
33
* e.g. -2 >>> 1 would become 1111111111111111111111111111111, i.e. 2147483647*/
30
-
31
-
32
- // you need treat n as an unsigned value
33
- public int reverseBits (int n ) {
34
+
35
+ public static class Solution1 {
36
+ // you need treat n as an unsigned value
37
+ public int reverseBits (int n ) {
34
38
int res = 0 ;
35
39
for (int i = 0 ; i < 32 ; i ++) {
36
- res += n & 1 ;//get the most right bit each time
37
- n >>>= 1 ;//do UN-signed right shift by 1 each time
38
- if (i < 31 ) {
39
- res <<= 1 ;//shift this number to the left by 1 each time, so that eventually, this number is reversed
40
- }
40
+ res += n & 1 ;//get the most right bit each time
41
+ n >>>= 1 ;//do UN-signed right shift by 1 each time
42
+ if (i < 31 ) {
43
+ res <<=
44
+ 1 ;//shift this number to the left by 1 each time, so that eventually, this number is reversed
45
+ }
41
46
}
42
47
return res ;
43
- }
44
-
45
- /**follow-up: if this function is called many times, how to improve it?
46
- Divide the integer into 4 bytes,
47
- reverse each byte and then combine them into one in the end,
48
- use cache to store the reversed results for reuse if possible.*/
49
-
50
- public static void main (String ... strings ) {
51
- _190 test = new _190 ();
52
- // int n = 43261596;
53
- int n = 4 ;
54
- System .out .println ("original number : " + n );
55
- System .out .println ("original number in binary format: " + Integer .toBinaryString (n ));
56
- int result = test .reverseBits (n );
57
- System .out .println ("reversed bit result: " + result );
58
- System .out .println ("reversed bit result in binary format: " + Integer .toBinaryString (result ));
59
-
60
- // System.out.println(Integer.toBinaryString(4));
61
- // System.out.println(Integer.parseInt("11000", 2));
62
- // System.out.println(Integer.parseInt("00011", 2));
63
- // System.out.println(-2 >>> 1);
64
- // System.out.println(Integer.toBinaryString(-2 >>> 1));
65
- // System.out.println(Integer.toBinaryString(-2));
66
- // System.out.println(Integer.toBinaryString(-1));
67
- // System.out.println(Integer.toBinaryString(6));
48
+ }
68
49
}
69
50
}
0 commit comments