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;
}
Title | # Comments | Views | Activity |
---|---|---|---|
What is JNDI datasource in spring | 1 | 36 | |
java set up | 1 | 44 | |
wordappend challenge | 8 | 82 | |
PDFBox - convert image to PDF, PDF resolution | 3 | 59 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
6 Experts available now in Live!