public static String addiere(List<String> l) {
StringBuilder res = new StringBuilder();
for (String str : l) {
int res_index = res.length() - 1;
int carry = 0;
for (int i = str.length() - 1; i >= 0; i--, res_index--) {
if (res_index < 0) {
res.insert(0, '0');
res_index++;
}
res.setCharAt(res_index,
(char) ((carry ^ str.charAt(i) - '0' ^ res.charAt(res_index) - '0') + '0'));
carry = carry + (str.charAt(i) - '0') + (res.charAt(res_index) - '0') >= 2 ? 1 : 0;
}
if (carry == 1) {
for (; res_index >= 0 && res.charAt(res_index) == '1'; res_index--) {
res.setCharAt(res_index, '0');
}
if (res_index < 0) {
res.insert(0, '1');
} else {
res.setCharAt(res_index, '1');
}
}
}
return res.toString();
}
public static void main(String[] args) {
System.out.println(addiere(new ArrayList<String>(Arrays.asList(
Integer.toBinaryString(1),
Integer.toBinaryString(2),
Integer.toBinaryString(3),
Integer.toBinaryString(4),
Integer.toBinaryString(5),
Integer.toBinaryString(6),
Integer.toBinaryString(7),
Integer.toBinaryString(3)))));
}