ALU { public String integerDivision (String operand1, String operand2, int length) { String formattedOperand1 = complementLength(operand1, length); String formattedOperand2 = complementLength(operand2, length); if (formattedOperand2.indexOf("1")==-1){ return "NaN"; } String thatTwoRegisters = repeatedString(formattedOperand1.substring(0,1), length)+formattedOperand1; String minusOperand2 = negation(formattedOperand2); boolean isOverflow = false; boolean nextPlus = false; char Qn; if (isSignTheSame(formattedOperand1, formattedOperand2)){ thatTwoRegisters = adder(thatTwoRegisters.substring(0,length), minusOperand2, '1', length).substring(1) + thatTwoRegisters.substring(length); if (isSignTheSame(thatTwoRegisters, formattedOperand2)){ Qn = '1'; nextPlus = false; isOverflow = true; } else{ Qn = '0'; nextPlus = true; } }else{ thatTwoRegisters = adder(thatTwoRegisters.substring(0,length), formattedOperand2, '0',length).substring(1) + thatTwoRegisters.substring(length); if (isSignTheSame(thatTwoRegisters, formattedOperand2)){ Qn = '1'; nextPlus = false; } else{ Qn = '0'; isOverflow = true; nextPlus = true; } } thatTwoRegisters = thatTwoRegisters.substring(1)+Qn; for(int i=0;i<length;i++){ if (!nextPlus){ thatTwoRegisters = adder(thatTwoRegisters.substring(0, length), minusOperand2, '1', length).substring(1) + thatTwoRegisters.substring(length); }else{ thatTwoRegisters = adder(thatTwoRegisters.substring(0, length), formattedOperand2, '0', length).substring(1) + thatTwoRegisters.substring(length); } if (isSignTheSame(thatTwoRegisters, formattedOperand2)){ Qn='1'; nextPlus = false; }else{ Qn='0'; nextPlus = true; } if (i!=length-1){ thatTwoRegisters = thatTwoRegisters.substring(1) + Qn; } } String left = thatTwoRegisters.substring(0,length); String result = thatTwoRegisters.substring(length+1)+Qn; if (!isSignTheSame(formattedOperand1, formattedOperand2)){ result = oneAdder(result).substring(1); } if (!isSignTheSame(left, formattedOperand1)){ if (isSignTheSame(formattedOperand1,formattedOperand2)){ left = adder(left, formattedOperand2, '0', length).substring(1); }else{ left = adder(left, minusOperand2, '1',length).substring(1); } } if (integerSubtraction(left, operand2, length).substring(1).equals(repeatedString("0", length))){ left = repeatedString("0",length); result = oneAdder(result).substring(1); } if (integerAddition(left, operand2, length).substring(1).equals(repeatedString("0", length))){ left = repeatedString("0",length); result = integerAddition(result, repeatedString("1", length),length).substring(1); } isOverflow = isOverflow || (result.equals("1"+repeatedString("0", length-1))); return (isOverflow ? "1" : "0") + result+left; }  static void main(String[] args); static final boolean floatIsInfinity(String input, int eLength, int sLength); static final String complementLength(String binary, int length); static final String repeatedString(String str, int times); static final String bitOp(String oneBitOperand1, String oneBitOperand2, BinaryOperator<Boolean> op); static final boolean isSignTheSame(String binary1, String binary2); static String movePoint(String operand, int offset); static final char charOp(BinaryOperator<Boolean> op, Character... bits); static final String stringOp(String operand1, String operand2, BinaryOperator<Boolean> op); static final boolean xor(boolean operand1, boolean operand2); static int minBinaryLong(int number); static String integerBinRepresentationInMinLength(String decimalNumber); static String get2sComplement(String binaryNumber, boolean isNegative); String integerRepresentation(String number, int length); String floatRepresentation(String number, int eLength, int sLength); String ieee754(String number, int length); String integerTrueValue(String operand); String floatTrueValue(String operand, int eLength, int sLength); String negation(String operand); String leftShift(String operand, int n); String logRightShift(String operand, int n); String ariRightShift(String operand, int n); String fullAdder(char x, char y, char c); String claAdder(String operand1, String operand2, char c); String oneAdder(String operand); String adder(String operand1, String operand2, char c, int length); String integerAddition(String operand1, String operand2, int length); String integerSubtraction(String operand1, String operand2, int length); String integerMultiplication(String operand1, String operand2, int length); String integerDivision(String operand1, String operand2, int length); String signedAddition(String operand1, String operand2, int length); String floatAddition(String operand1, String operand2, int eLength, int sLength, int gLength); String floatSubtraction(String operand1, String operand2, int eLength, int sLength, int gLength); String floatMultiplication(String operand1, String operand2, int eLength, int sLength); String floatDivision(String operand1, String operand2, int eLength, int sLength); String trueFormDivision(String operand1, String operand2, int length); static final BinaryOperator<Boolean> booleanAnd; static final BinaryOperator<Boolean> booleanOr; static final BinaryOperator<Boolean> booleanXOR; }
@Test public void testIntegerDivision() { ALU alu = new ALU(); assertEquals("00000000100000001", alu.integerDivision("0100", "0011", 8)); }
@Test public void testIntegerDivision1(){ assertEquals("110000000",ALU.integerDivision("1000", "11", 4)); }
@Test public void testIntegerDivision2(){ assertEquals("NaN",ALU.integerDivision("1000", "000", 4)); }
@Test public void testIntegerDivision3(){ assertEquals("000001111",ALU.integerDivision("1111", "0110", 4)); }
@Test public void testIntegerDivision4(){ assertEquals("011101111",ALU.integerDivision("1001", "0011", 4)); }