package com.fishercoder.solutions;

/**
 * 190. Reverse Bits
 * Reverse bits of a given 32 bits unsigned integer.

 Example:
 Input: 43261596
 Output: 964176192

 Explanation: 43261596 represented in binary as 00000010100101000001111010011100,
 return 964176192 represented in binary as 00111001011110000010100101000000.

 Follow up:
 If this function is called many times, how would you optimize it?
 */

public class _190 {
    /**delimiting the binary string into 4 bits array will make it easier to see/visualize:
     * original binary format:
     * 0000,0010,1001,0100,0001,1110,1001,1100,
     * after reversing, becomes:
     * 0011,1001,0111,1000,0010,1001,0100,0000
     * The most right side digit shifted to the most left side, the 2nd right side digit shifted to the 2nd left side, so forth..*/

    /**This post: http://stackoverflow.com/questions/2811319/difference-between-and
     * gives a good explanation between logical right shift: ">>>" and arithmetic right shift: ">>".
     * Basically, ">>" preserves the most left bit and treats it as the sign for this number,
     * e.g. -2 represented in 8 bits is 11111110, thus -2 >> 1 will become 11111111, i.e. -1
     * notice its sign bit (the most left one bit) is preserved
     * However, logical right shift ">>>" doesn't care about the first bit on the most left,
     * it simply shifts every bit to the right.
     * e.g. -2 >>> 1 would become 1111111111111111111111111111111, i.e. 2147483647*/

 public static class Solution1 {
      // you need treat n as an unsigned value
      public int reverseBits(int n) {
        int res = 0;
        for (int i = 0; i < 32; i++) {
          res += n & 1;//get the most right bit each time
          n >>>= 1;//do UN-signed right shift by 1 each time
          if (i < 31) {
            res <<=
                1;//shift this number to the left by 1 each time, so that eventually, this number is reversed
          }
        }
        return res;
      }
    }
}

@@ -33,378 +33,8 @@
 s;%0A%0A
-/**%0A * 190. Reverse Bits%0A * Reverse bits of a given 32 bits unsigned integer.%0A%0A Example:%0A Input: 43261596%0A Output: 964176192%0A%0A Explanation: 43261596 represented in binary as 00000010100101000001111010011100,%0A return 964176192 represented in binary as 00111001011110000010100101000000.%0A%0A Follow up:%0A If this function is called many times, how would you optimize it?%0A */%0A%0A
 publ
@@ -440,16 +440,24 @@
 %0A    /**
+%0A     * 
 This pos
@@ -1100,20 +1100,29 @@
 47483647
+%0A     
 */%0A%0A
+   
  public 
@@ -1152,16 +1152,18 @@
 %7B%0A      
+ 
+ 
 // you n
@@ -1197,24 +1197,26 @@
 value%0A      
+  
 public int r
@@ -1243,16 +1243,20 @@
         
+  
+  
 int res 
@@ -1268,16 +1268,20 @@
         
+  
+  
 for (int
@@ -1299,24 +1299,30 @@
  32; i++) %7B%0A
+      
           re
@@ -1377,16 +1377,22 @@
         
+  
+    
 n %3E%3E%3E= 1
@@ -1444,16 +1444,22 @@
         
+     
+ 
 if (i %3C 
@@ -1480,16 +1480,36 @@
     
-res %3C%3C=%0A
+        res %3C%3C=%0A            
     
@@ -1628,20 +1628,16 @@
     
-%7D%0A  
       %7D%0A
     
@@ -1628,24 +1628,42 @@
           %7D%0A
+            %7D%0A    
         retu
@@ -1670,16 +1670,18 @@
 rn res;%0A
+  
       %7D%0A

refactor 190