# Java long division in finite field 2

How do I do a long division in galois field 2 in java?
###### Who is Participating?

Author Commented:
``````public static int gf2LongDivideQ(int dividend, int divisor) {

int dividendLength = getLengthOf(dividend);
int divisorLength = getLengthOf(divisor);

if (divisorLength > dividendLength) {
return -1;
}

int tempDividend = 0;

int quotient = 0;

for (int i = 1; i <= dividendLength; i++) {
int bitAtPositionI = getBit(dividend, i);

tempDividend = (tempDividend << 1) + bitAtPositionI;

if (getLengthOf(tempDividend) < divisorLength) { // cannot divide
// append '0' to end of quotient
quotient = (quotient << 1);
} else {
// perform xor operation
tempDividend = tempDividend ^ divisor;

// append '1' to end of quotient
quotient = (quotient << 1) + 1;
}
}

return quotient;
}

public static int getLengthOf(int input) {
int length = 0;
while (input != 0) {
input = input >> 1;
length++;
}
return length;
}

public static int getBit(int input, int index) {
input = input >> (getLengthOf(input) - index); // shift out bits to the right by (length - index)
input = input & 0x1; // get right most bit
return input;
}
``````
0

Chief Technology OfficerCommented:
humansg, before we get started, is this for a homework assignment? If so, do you know how to work out the long division manually as I can help you translate that to Java, thus helping you "learn" versus just spitting out an answer...
0

Author Commented:
yes, it is part of an assignment which I need to call for part of euclid algorithm..
yes i know how to do the long division manually.
0

Chief Technology OfficerCommented:
Okay. Great. Please post what you have for the calculation as you understand it in your attempt in Java. If you are getting any compilation or run-time errors, then you should post details on those. For run-time errors, posting the full stack trace in a code snippet is very useful. If the issue is logical, i.e., everything compiles and runs but you are not getting the expected results, then ensure to post what you expect alongside what you are getting currently.

From there, we can guide you through correcting the code.
0

Author Commented:
Coded out myself. Should be able to work..
0

Author Commented:
accepting own solution..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.