"101011001101110101" + "111110010000111010" = "1121201011112101111"
static String binarnoSestej(String s, String b){ long st1 = Long.parseLong(s); long st2 = Long.parseLong(b); int i = 0; int prenos = 0; int[] koncnoSt = new int[s.length() + b.length()]; while (st1 != 0 || st2 != 0) { koncnoSt[i++] = (int) (prenos + (st1 % 10 + st2 % 10) % 2); prenos = (int) ((st1 % 10 + st2 % 10 + prenos) / 2); st1 = st1 / 10; st2 = st2 / 10; } if (prenos != 0) { koncnoSt[i++] = prenos; } --i; String bit = ""; while (i >= 0) { bit = bit + koncnoSt[i--]; } return bit; }
radix
Long.parseLong
long st1 = Long.parseLong(s, 2); long st2 = Long.parseLong(b, 2);
Quote:Why would my code add 2 when doing binary addition?
koncnoSt[i++] = (int) (prenos + (st1 % 10 + st2 % 10) % 2);
koncnoSt[i++] = (int) ((prenos + st1 % 10 + st2 % 10) % 2);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)