1

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--;
        }
3

1 Answer 1

4

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));
Sign up to request clarification or add additional context in comments.

9 Comments

I will have to divide an 1400 bit integer by 16 bit integer. Will it work with this?
BigInteger can represent and perform operations on any size integer.
Actually I need the remainder as well, and it only gives me the quotient. Am I missing something?
@JaniBela look at the methods that the BigInteger class provides to you: docs.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html - i am sure you can figure out how to get both the remainder and quotient. you should always consult the API when dealing with an unfamiliar class.
Thnak you very much, now I can try it for my CRC :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.