Original post https://leetcode.com/discuss/8897/share-my-o-log-n-solution-using-bit-manipulation

This is to make the code simpler by two principles

- avoid overflow by replacing * with /, + with -
- minimize calculations

```
public int mySqrt(int x) {
int res = 0;
for (int mask = 1 << 15; mask != 0; mask >>>= 1) {
int next = res | mask; //set bit
if (next <= x / next) res = next;
}
return res;
}
```