Reverse Bits of Integer (16-bit)
by Isai Damier, Android Engineer @ Google

/**************************************************************************
 * Author: Isai Damier
 * Title: Reverse Bits of Int (8-bit)
 * Project: geekviewpoint
 * Package: algorithms
 *
 * Statement:
 *   Given an integer, reverse its bit sequence.
 *
 * Sample Input:  00000000000000001111111111111110
 * Sample Output: 01111111111111110000000000000000
 *
 * Technical Details:
 *   It is necessary to know whether the decimal number being passed as
 *   input is of type byte (8-bit) or short (16-bit) or int (32-bit) or
 *   long (64-bit): because Java will discard leading zeroes. For instance,
 *   if x = 0011010, Java will trim it to 11010 and then cause the reverse
 *   to look like 1011. Under such circumstances the reverseBits operation
 *   would not be reversible.
 *
 *   To keep things simple, the presented algorithm treats int (32-bit)
 *   inputs.
 **************************************************************************/ 
 public int reverseBitsInt(int x) {
  int intSize = 32;
  int y=0;
  for(int position=intSize-1; position>0; position--){
    y+=((x&1)<<position);
    x >>= 1;
  }
  return y;
}
import org.junit.Test;
import static org.junit.Assert.*;

public class BitwiseTest {

 /**
   * Test of reverseBitsInt method, of class Bitwise.
   */
  @Test
  public void testReverseBitsInt() {
    System.out.println(""reverseBits"");
    String a = ""00000000000000001111111111111110"";
    String b = ""01111111111111110000000000000000"";
    int x = Integer.parseInt(a,2);
    int r = Integer.parseInt(b,2);
    Bitwise bits = new Bitwise();
    assertEquals(r, bits.reverseBitsInt(x));
    assertEquals(x, bits.reverseBitsInt(r));
  }
}