Division by Different way


Description:
This is multiple way do the division. But mainly focusing on Booth's Algorithms' and recursive methods using algorithm



Code 1:
?
package com.kartik;
/**
 * 
 * @author kartik 
 * This is multiple way division algorithm. Mainly focusing on Booth's algorithms
 */
package com.kartik;

public class Division {

    public static final long division(int a, int b) {
        long result = ((long) a) / ((long) b);
        return result;
    }

    public static final long divisionUsingLoop(int a, int b) {
        int absoluteFirstData = Math.abs(a);
        int absoluteSecondData = Math.abs(b);

        long temp = absoluteFirstData;
        long result = 0;
        while (temp >= 0) {
            temp -= absoluteSecondData;
            if (temp >= 0)
                result++;
        }
        return (a > 0 && b > 0 || a < 0 && b < 0) ? result : -result;
    }

    public static final long divisionUsingRecursion(int a, int b) {
        int absoluteFirstData = Math.abs(a);
        int absoluteSecondData = Math.abs(b);

        long result = 1;
        int diff = absoluteFirstData - absoluteSecondData;
        if (diff > 0 && diff <= 1) {
            return result;
        } else if (diff < 0) {
            return 0;
        }

        result += divisionUsingRecursion(diff, absoluteSecondData);
        return (a > 0 && b > 0 || a < 0 && b < 0) ? result : -result;
    }

    public static final long divisionUsingMultiplication(int a, int b) {
        int absoluteFirstData = Math.abs(a);
        int absoluteSecondData = Math.abs(b);

        int temp = absoluteSecondData;
        int counter = 0;
        while (temp <= absoluteFirstData) {
            temp = temp << 1;
            counter++;
        }
        absoluteFirstData -= absoluteSecondData << (counter - 1);
        long result = (long) Math.pow(2, counter - 1);
        if (absoluteSecondData <= absoluteFirstData)
            result += divisionUsingMultiplication(absoluteFirstData, absoluteSecondData);
        return (a > 0 && b > 0 || a < 0 && b < 0) ? result : -result;
    }

    public static final long divisionUsingShift(int a, int b) {
        int absoluteFirstData = Math.abs(a);
        int absoluteSecondData = Math.abs(b);
        int tempA, tempB, counter;

        long result = 0L;
        while (absoluteFirstData >= absoluteSecondData) {
            tempA = absoluteFirstData >> 1; // Right shift "a"
            tempB = absoluteSecondData;
            counter = 1;
            while (tempA >= tempB) { // Double "tempB" until it's larger than
                                     // "tempA"
                tempB <<= 1;
                counter <<= 1; // Double the counter
            }
            absoluteFirstData -= tempB; // Subtract "tempB" from "a"
            result += counter; // Add counter (2^number of left shifts)
        }
        return (a > 0 && b > 0 || a < 0 && b < 0) ? result : -result;
    }

    public static final long divisionUsingLogs(int a, int b) {
        long absoluteFirstData = Math.abs(a);
        long absoluteSecondData = Math.abs(b);
        double logBase10A = Math.log10(absoluteFirstData);
        double logBase10B = Math.log10(absoluteSecondData);
        double powOf10 = Math.pow(10, (logBase10A - logBase10B));
        long result = (long) Math.floor(powOf10);
        return (a > 0 && b > 0 || a < 0 && b < 0) ? result : -result;
    }
}

Example 1 divisionUsingShift:





Example 2 divisionUsingMultiplication with recursion:






Previous
Next Post »