ALU { public String signedAddition (String operand1, String operand2, int length) { int valueLength = length+1; StringBuilder result = new StringBuilder(); String absOperand1 = repeatedString("0", length-operand1.length()+2) + operand1.substring(1); String absOperand2 = repeatedString("0", length-operand2.length()+2) + operand2.substring(1); if (isSignTheSame(operand1, operand2)){ String addResult = adder(absOperand1, absOperand2, '0', valueLength).substring(1); result.append(addResult.charAt(0)); result.append(operand1.charAt(0)).append(addResult.substring(1)); }else{ String minusResult = adder("0"+absOperand1, "0"+negation(absOperand2), '1', valueLength+1); char sign = operand1.charAt(0); char highestCarry = minusResult.charAt(0); minusResult = minusResult.substring(3); if (highestCarry=='0'){ minusResult = get2sComplement(minusResult, true); sign = sign=='0' ?'1':'0'; } result.append('0').append(sign).append(minusResult); } return result.toString(); }  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 testSignedAddition() { ALU alu = new ALU(); assertEquals("0100000111", alu.signedAddition("1100", "1011", 8)); }
@Test public void testSignedAddition1(){ assertEquals("001000",ALU.signedAddition("10011", "01011", 4)); }
@Test public void testSignedAddition2(){ assertEquals("101001",ALU.signedAddition("01111", "01010", 4)); }
@Test public void testSignedAddition3(){ assertEquals("0000001000",ALU.signedAddition("10011", "01011", 8)); }
@Test public void testSignedAddition4(){ assertEquals("010011",ALU.signedAddition("0011", "1110", 4)); }