File tree Expand file tree Collapse file tree 1 file changed +7
-8
lines changed Expand file tree Collapse file tree 1 file changed +7
-8
lines changed Original file line number Diff line number Diff line change 19
19
* Tags: Bit Manipulation
20
20
*/
21
21
class ReverseBits {
22
+
22
23
public static void main (String [] args ) {
23
24
ReverseBits r = new ReverseBits ();
24
- // int a = 43261596;
25
- // System.out.println(r.reverseBits(a));
26
- // System.out.println(r.reverseBitsOpt(a));
25
+ int a = 43261596 ;
26
+ System .out .println (r .reverseBits (a ));
27
+ System .out .println (r .reverseBitsOpt (a ));
27
28
28
29
int b = 1 ;
29
- // System.out.println(r.reverseBits(b));
30
+ System .out .println (r .reverseBits (b ));
30
31
System .out .println (r .reverseBitsOpt (b ));
31
32
}
32
33
@@ -40,10 +41,8 @@ public static void main(String[] args) {
40
41
*/
41
42
public int reverseBits (int n ) {
42
43
int res = 0 ;
43
- for (int i = 0 ; i < 32 ; i ++) {
44
- res = (res << 1 ) ^ (n & 1 ); // add first bit of n to last bit of res
45
- n >>>= 1 ; // unsigned shift to right
46
- }
44
+ // concat n's ith digit with res
45
+ for (int i = 0 ; i < 32 ; i ++) res = (res << 1 ) ^ ((n >>> i ) & 1 );
47
46
return res ;
48
47
}
49
48
You can’t perform that action at this time.
0 commit comments