Java中的8位全加器

时间:2014-09-30 15:53:24

标签: java

在'eightbitfulladder'函数中,我试图补充sumno,这是添加正数和负数的结果。我发现它在'convertobyte'函数中发送的数字与sumno不同计算。这很奇怪。有人能解释我发生了什么。

import static java.lang.Math.pow;

public class CAModifiedBoothsMultiplier {
    public byte byteArrayTobyte(byte[] b) {
        byte value = 0;

        for (byte i = 0; i < 8; i++) {
            value += (b[i] * (pow(2, i)));
        }
        return value;
    }

    public byte[] twosComplement(byte x) {
        byte y = (byte) (~x + 1);

        byte mask = 1;
        mask = (byte) (mask << 7);
        byte num[] = new byte[10];
        byte i = 0, num1 = 0;

        for (i = 0; i < 8; i++) {
            if ((y & mask) == 0)
                num1 = 0;
            else
                num1 = 1;

            y = (byte) (y << 1);

            num[i] = num1;
        }

        return num;

    }

    public byte[] saveByte(byte number) {
        byte mask = 1;
        mask = (byte) (mask << 7);
        byte num[] = new byte[10];
        byte num1 = 0;
        byte i = 0;

        for (i = 0; i < 8; i++) {
            if ((number & mask) == 0)
                num1 = 0;
            else
                num1 = 1;

            number = (byte) (number << 1);
            num[i] = num1;
        }
        return num;
    }

    public byte[] eightbitFullAdder(byte a, byte b, byte cin) {
        byte sum = 0, temp1 = 0, cout = 0;
        byte sumno[] = new byte[10];
        byte ain[] = saveByte(a);

        byte bin[] = new byte[10];

        if (cin == 1) {
            bin = twosComplement(b);
            cin = 0;
        } else {
            bin = saveByte(b);
        }

        System.out.print("The number entered is, a :");

        for (int i = 0; i < 8; i++) {
            System.out.print(ain[i]);
        }
        System.out.println("\n");

        System.out.print("The number entered is, b :");

        for (int i = 0; i < 8; i++) {
            System.out.print(bin[i]);
        }
        System.out.println("\n");

        for (int i = 7; i >= 0; i--) {
            temp1 = (byte) (ain[i] ^ bin[i]);
            sum = (byte) (temp1 ^ cin);
            sumno[i] = sum;

            cout = (byte) ((ain[i] & bin[i]) | (cin & temp1));

            if (i != 0)
                cin = cout;

        }

        for (int i = 0; i < 8; i++) {
            System.out.print(sumno[i]);
        }
        System.out.println("\n");

        if (sumno[0] == 1) {
            byte[] sumnocomp = new byte[10];

            byte temp2 = 0;
            temp2 = byteArrayTobyte(sumno);
            System.out.print(temp2);

            sumnocomp = twosComplement(temp2);

            return sumnocomp;
        } else {
            return sumno;

        }
    }

    public static void main(String args[]) {
        CAModifiedBoothsMultiplier mbm = new CAModifiedBoothsMultiplier();
        byte x = 5;
        byte complementedno[];
        complementedno = mbm.twosComplement(x);

        for (int i = 1; i <= 8; i++)
            System.out.print(complementedno[i]);

        System.out.println("\n");

        byte a = 6, b = 8, cin = 1;

        byte fulladder[] = mbm.eightbitFullAdder(a, b, cin);

        System.out.print("The sum of numbers entered is :");
        for (int i = 0; i < 8; i++) {
            System.out.print(fulladder[i]);
        }
        System.out.println("\n");
    }

}

0 个答案:

没有答案