class 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); //+1 for compensating sign bit, +1 for making it positive for adder
		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)); //overflow
			
			result.append(operand1.charAt(0)).append(addResult.substring(1));
			
		}else{
			String minusResult = adder("0"+absOperand1, "0"+negation(absOperand2), '1', valueLength+1); //overflow bit indicates the highest carry
			char sign = operand1.charAt(0);
			char highestCarry = minusResult.charAt(0);
			minusResult = minusResult.substring(3);
			if (highestCarry=='0'){
				//no carry, result is negative, 
				minusResult = get2sComplement(minusResult, true);
				sign = sign=='0' ?'1':'0';
			}
			result.append('0').append(sign).append(minusResult);
		}
		return result.toString();
	}

}