Demo entry 1467079

java

   

Submitted by anonymous on Mar 06, 2015 at 19:49
Language: Java. Code size: 7.4 kB.

package nonrestoringdivision;

import java.util.Scanner;
import java.util.Stack;
import java.util.Scanner;
import java.util.Stack;

public class NonRestoringDivision {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        Binary AQ, A, B, Q;
        Character E, ZERO, ONE;
        ZERO = E = '0';
        ONE = '1';
        int len, Dr, Bs = 0, Dn, As = 0;
        System.out.print("Enter Length of Dr : ");
        len = s.nextInt();
        System.out.print("Enter Dr : ");
        Dr = s.nextInt();
        if (Dr < 0) {
            Bs = 1;
            Dr = 0 - Dr;
        }
        B = new Binary(Dr, len);

        System.out.print("Enter Dnd : ");
        Dn = s.nextInt();
        if (Dn < 0) {
            As = 1;
            Dn = 0 - Dn;
        }

        AQ = new Binary(Dn, len * 2);
        Q = AQ.getPart(AQ.length / 2, AQ.length);
        A = AQ.getPart(0, AQ.length / 2);

        System.out.println("=========================");
        AQ.display();
        System.out.println("%");
        B.display();

        if (A.getDecimal() > B.getDecimal()) {
            System.out.println("Divide OverFlow Error");
            return;
        } else {

            E = A.lShift(Q.lShift(ZERO));
            A = Binary.subtract(A, B);
            do {
                E = A.getMSB();
                if (E.equals(ONE)) {
                    len--;
                    if (len != 0) {
                        E = A.lShift(Q.lShift(ZERO));
                        A = Binary.add(A, B);
                        continue;
                    } else {
                        break;
                    }
                } else {
                    Q = Binary.add(Q, new Binary(1, Q.length));
                    len--;
                    if (len != 0) {
                        E = A.lShift(Q.lShift(ZERO));
                        A = Binary.subtract(A, B);
                        continue;
                    } else {
                        break;
                    }
                }
            } while (true);

            if (E.equals(ONE)) {
                A = Binary.add(A, B);
            }

        }
        System.out.println("=========================");
        int Quo = Q.getDecimal();
        if (!(As == 0 && Bs == 0) || ((As == 1 && Bs == 1))) {
            Quo = 0 - Quo;
        }
        System.out.println("Rem : " + A.getDecimal() + " Quo : " + Quo);
    }

}

class Binary {

    Character binary[];
    Integer length;
    static final Character ZERO = '0', ONE = '1';

    public Binary(int decimal, int length) {

        this.length = length;
        Stack s = new Stack();
        if (decimal < 0) {
            decimal += Math.pow(2, length);
        }

//bin
        while (decimal != 0) {
            s.push((char) (decimal % 2 + 48));
            decimal /= 2;
        }

        while (s.size() < length) {
            s.push('0');
        }
        binary = new Character[length];
        int i = 0;
        while (!s.empty()) {
            binary[i++] = (Character) s.pop();
        }

    }

    public Binary(Character[] binary) {
        this.binary = binary;
        length = binary.length;
    }

    void display() {
        for (int i = 0; i < length; i++) {
            System.out.print(binary[i]);
        }
        System.out.println("");
    }

    Character rShift(Character c) {
        Character end = binary[length - 1];
        for (int i = length - 1; i > 0; i--) {
            binary[i] = binary[i - 1];
        }
        binary[0] = c;
        return (end);
    }

    Character lShift(Character c) {
        Character start = binary[0];
        for (int i = 0; i < length - 1; i++) {
            binary[i] = binary[i + 1];
        }
        binary[length - 1] = c;
        return (start);
    }

    Character getLSB() {
        return (binary[length - 1]);
    }

    Character getMSB() {
        return (binary[0]);
    }

    Character getBit(int i) {
        return (binary[i]);
    }

    void setBit(Character bit, int i) {
        binary[i] = bit;
    }

    static Binary add(Binary A, Binary B) {

        Binary sum = new Binary(0, A.length);
        Character carry = '0';
        for (int i = A.length - 1; i >= 0; i--) {
            if (carry.equals(ZERO) && A.getBit(i).equals(ZERO) && B.getBit(i).equals(ZERO)) {
                sum.setBit(ZERO, i);
                carry = ZERO;
            } else if (carry.equals(ONE) && A.getBit(i).equals(ZERO) && B.getBit(i).equals(ZERO)) {
                sum.setBit(ONE, i);
                carry = ZERO;
            } else if (carry.equals(ZERO) && A.getBit(i).equals(ONE) && B.getBit(i).equals(ZERO)) {
                sum.setBit(ONE, i);
                carry = ZERO;
            } else if (carry.equals(ONE) && A.getBit(i).equals(ONE) && B.getBit(i).equals(ZERO)) {
                sum.setBit(ZERO, i);
                carry = ONE;
            } else if (carry.equals(ZERO) && A.getBit(i).equals(ZERO) && B.getBit(i).equals(ONE)) {
                sum.setBit(ONE, i);
                carry = ZERO;
            } else if (carry.equals(ONE) && A.getBit(i).equals(ZERO) && B.getBit(i).equals(ONE)) {
                sum.setBit(ZERO, i);
                carry = ONE;
            } else if (carry.equals(ZERO) && A.getBit(i).equals(ONE) && B.getBit(i).equals(ONE)) {
                sum.setBit(ZERO, i);
                carry = ONE;
            } else if (carry.equals(ONE) && A.getBit(i).equals(ONE) && B.getBit(i).equals(ONE)) {
                sum.setBit(ONE, i);
                carry = ONE;
            }
        }
        return (sum);
    }

    static Binary subtract(Binary A, Binary B) {
        return (add(A, B.twosComplimant()));
    }

    public Binary twosComplimant() {
        Character onesCompliment[] = new Character[length];

        //1's compliment
        for (int j = 0; j < length; j++) {
            if (binary[j].equals(ZERO)) {
                onesCompliment[j] = ONE;
            } else {
                onesCompliment[j] = ZERO;
            }
        }
        //2's complement by adding 1
        Character[] add1 = new Character[length];
        for (int j = 0; j < length - 1; j++) {
            add1[j] = '0';
        }
        add1[length - 1] = '1';
        return (add(new Binary(onesCompliment), new Binary(add1)));
    }

    int getDecimal() {
        int mag = 1;
        int dec = 0;
        Character bin[];

        bin = binary;
        for (int i = length - 1; i >= 0; i--, mag *= 2) {
            if (bin[i].equals(ONE)) {
                dec += mag;
            }
        }
        return (dec);
    }

    static int combineResult(Binary A, Binary B) {
        int dec;
        dec = (int) (A.getDecimal() * Math.pow(2, A.length) + B.getDecimal());
        return (dec);
    }

    static Binary combine(Binary A, Binary B) {
        Character[] combined = new Character[A.length * 2];
        System.arraycopy(A.binary, 0, combined, 0, A.length);
        System.arraycopy(B.binary, 0, combined, A.length, B.length);
        return (new Binary(combined));
    }

    Binary getPart(int start, int end) {

        Binary part = new Binary(0, this.length / 2);
        for (int i = start, j = 0; i < end; i++, j++) {
            part.setBit(this.getBit(i), j);
        }
        return (part);
    }

    void setLSB() {
        binary[0] = '1';
        display();
    }
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).