code: public static int nextPowerOfTwo(int x) {
      assert x < 0x10000;
  
      int bit = 0x8000, highest = -1, count = 0;
      for (int i = 15; i >= 0; --i, bit >>= 1) {
        if ((x & bit) != 0) {
          ++count;
          if (highest == -1) {
            highest = i;
          }
        }
      }
      if (count <= 1) {
        return 0;
      }
      return 1 << (highest + 1);
    }
docs: Returns the next largest power of two, or zero if x is already a power of two.
  
  TODO(jgw): Is there no better way to do this than all this bit twiddling?