public static int fixedPoint32Sqrt(int input, int highBitLength) { if (input < 0) return -1; int top = 0; int shiftAmount = 30; int currentPair = input >> shiftAmount; switch (currentPair) { case 0: top = 0; break; case 1: // case 2: // case 3: top = 1; break; } int subResult = currentPair - top; do { int tempRem = (top * 2) << 1; tempRem |= 1; if (tempRem > subResult) tempRem = 0; top = top << 1 | (tempRem != 0 ? 1 : 0); if (shiftAmount == 0) break; shiftAmount -= 2; currentPair = (input >> shiftAmount) & 0x00000003; subResult = (subResult - tempRem) << 2; subResult |= currentPair; } while (true); return top; }