Converting back and forth between string and ints to do math is inefficient. Do the math in integers and use formatting to display binary: MOD = 1 << 16 def ones_comp_add16(num1,num2): result = num1 + num2 return result if result < MOD else (result+1) % MOD n1 = 0b1010001111101001 n2...

javascript,bitwise-operators,twos-complement,ones-complement

Note - I don't think this is the problem, but it's a problem. You've left off a set of { } else { document.getElementById("binary").innerHTML = aNumber.toString(2); document.getElementById("ones").innerHTML = (~aNumber).toString(2); document.getElementById("twos").innerHTML = (-aNumber).toString(2); } Without those, the second two lines were outside the scope of influence of the if else statement,...

algorithm,bit-manipulation,formula,twos-complement,ones-complement

you cannot calculate any formula with numbers in 2 different bases, you have to use their decimal representation (or an other representation you can handle) I'll give it a try in dec: 1042 (base 5) = 1* 5^3 + 4* 5^1 + 2 = 125 + 20 + 2 =...