Jani Bela
Jani Bela

Reputation: 1670

Binary divison java implementation

It might be a weird question, but I havent found any binary division java implementation browsing the internet. I will use it for CRC16 coding, so converting to decimal is not a solution. I understand the method on paper, but I am a beginner and since it's very important I wouldn't want to make it wrong. The only thing I found is a CRC.java on the code.google.com which uses binary division, but even if I only use the division part of that (removing the other parts) I dont get the desirable value.

Anybody can show me a java implementation of it? I'd appreciate it very much! Thanks in advance

The code what I found:

public class CRC { private String data, divisor;

public CRC(String d, String di) {
    this.data = d;
    this.divisor = di;
}
public String getRemainder(String data, String divisor) {
    int x = 1, z = divisor.length(), j = 0, i;
    String data2 = "", strOfZeros = "";
    int y = divisor.length() - 1;
    /* This is to get correct amount of zero's onto the end of the data */
    while (y > 0) {
        data += "0";
        y--;
    }
    // Main part of method, this is the long division of Binary numbers.
    needToExit: for (i = x, j = 1; i < z && z <= data.length(); i++, j++) {
        if (z == data.length() && data2.charAt(0) == '1') {
            strOfZeros = "";
            for (i = 1; i < divisor.length(); i++) {    
                if (data2.charAt(i) == divisor.charAt(i))
                    strOfZeros += '0';
                else
                    strOfZeros += '1';
            }
            data2 = strOfZeros;
            break needToExit;
        }

        if (data.charAt(i) == divisor.charAt(j))
            data2 += "0";
        else
            data2 += "1";

        if (i == divisor.length() - 1) {
            data2 += data.charAt(z);
            x++;
            z++;
            // i = x;
            j = 0;

            // when first bit is a 0
            while (data2.charAt(0) != '1' && i == divisor.length() - 1) {
                for (i = 1; i < divisor.length(); i++) {
                    if (data2.charAt(i) == '0')
                        strOfZeros += "0";
                    else
                        strOfZeros += "1";
                }
                strOfZeros += data.charAt(z);
                data2 = strOfZeros;
                strOfZeros = "";
                x++;
                z++;
                i = x;
            }
        }

    }
    return data2;
}
public String getDataPlusCRC(String data){
    String str = data.concat(getRemainder(this.data, this.divisor));
    return str;
}

}

The getRemainder() method gives me a bad result when I try to divide to numbers. And this part is not necessary, because it needs for the CRC.

while (y > 0) {
            data += "0";
            y--;
        }

Upvotes: 1

Views: 8704

Answers (1)

Alex Lynch
Alex Lynch

Reputation: 951

BigInteger can do base 2 division;

    BigInteger divisor = new BigInteger("10", 2);
    BigInteger dividend = new BigInteger("100", 2);
    BigInteger result = dividend.divide(divisor);

    System.out.println(result.toString(2));

Upvotes: 4

Related Questions