# Javascript, my two additions are not producing the correct number

I'm writing a basic calculator for my computer science class that takes a number between 0 and 255 and converts it to binary, one's complement and two's complement. Me and another student worked on this together and we both got stuck in two expansions, it generates the wrong value. For example, if we enter 11, then the two's complement should be "1111 1011", but I get "1111 0101" and I'm not sure what is wrong?

I started a demo at jsbin.com and I hope someone here can really save my life (and my class) and tell me what my coding partner and I are doing wrong.

jsbin β http://jsbin.com/juqevomiliwo/1/edit

Also, I would really like to add some error checking, for example if the user enters a negative number or if the user enters a value that is not in the 0-255 range, can someone help me with that? I'd really like to pop up, say, "I'm sorry you need to enter a value between 0-255"

I apologize for asking so much, but I don't know where else to turn! I went to stackoverflow so many times to help me solve problems and this is my first question when I don't know how to solve!

Thank you very much for your time and your help!

-Martin

Here is the code:

```
<!DOCTYPE html>
<html>
<head>
<title>Group Project</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Group Project 1</h1>
<p>Enter a number between 0 and 255 <input id="theInput" type="text" /> <button onclick="doSomething()">Show Binary</button> </p>
<p>Unsigned Binary: <span id="binaryOutput"></span></p>
<p>One Complement: <span id="onesOutput"></span></p>
<p>Two Complement: <span id="twosOutput"></span></p>
<script>
function doSomething(){
var i = document.getElementById('theInput').value; //gets number from input box and sets it to i
var binary = pad(Number(i).toString(2), 8); //converts i to binary and pads with leading zeros until 8 digits long
var ones = binary.toString(2).replace(/1/g, 'a').replace(/0/g, '1').replace(/a/g, '0');
var twos = pad((Number(ones) + 1), 8);
document.getElementById('binaryOutput').innerHTML = fancy(binary); //puts the binary in the Unsigned Binary field
document.getElementById('onesOutput').innerHTML = fancy(ones); //puts the ones compliment in the One Complement field
document.getElementById('twosOutput').innerHTML = fancy(twos); //puts the twos compliment in the Two Complement field
}
//adds leading zeros to achieve width
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
//adds space every four digits
function fancy(b) {
return b.replace( /\d{4}/g, '$& ' ).replace( /\s$/,'');
}
</script>
</body>
```

source to share

In your string, the `twos = pad((Number(ones) + 1), 8);`

function `Number()`

does not convert the binary string to a number (it assumes the string is decimal).

You should use instead `twos = pad((parseInt(ones, 2) + 1).toString(2), 8);`

. See snippet below.

```
function doSomething(){
var i = document.getElementById('theInput').value; //gets number from input box and sets it to i
var binary = pad(Number(i).toString(2), 8); //converts i to binary and pads with leading zeros until 8 digits long
var ones = binary.replace(/1/g, 'a').replace(/0/g, '1').replace(/a/g, '0');
var twos = pad((parseInt(ones, 2) + 1).toString(2), 8);
document.getElementById('binaryOutput').innerHTML = fancy(binary); //puts the binary in the Unsigned Binary field
document.getElementById('onesOutput').innerHTML = fancy(ones); //puts the ones compliment in the One Complement field
document.getElementById('twosOutput').innerHTML = fancy(twos); //puts the twos compliment in the Two Complement field
}
//adds leading zeros to achieve width
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
//adds space every four digits
function fancy(b) {
return b.replace( /\d{4}/g, '$& ' ).replace( /\s$/,'');
}
```

```
<h1>Group Project 1</h1>
<p>Enter a number between 0 and 255
<input id="theInput" type="text" />
<button onclick="doSomething()">Show Binary</button>
</p>
<p>Unsigned Binary: <span id="binaryOutput"></span>
</p>
<p>One Complement: <span id="onesOutput"></span>
</p>
<p>Two Complement: <span id="twosOutput"></span>
</p>
```

source to share

Your main mistake is understanding one complement system. Basically, you need to decide if you have numbers from 0 to 255 or -127 to 127. The inversion is done only for negative numbers (the first bit on the left is 1) or for numbers above 127, and positive numbers are unchanged (or numbers from 0 to 127, there is also negative zero when all bits are set to 1, which is one's complement of 255).

So, one's complement to 11 is 11 or 0000 1011 and in the two's complement systems it doesn't change it either, because there is no need to add 1.

As soon as you talk about signed numbers and the unsigned number is above 127, it sets the most significant bit to 1, which indicates a negative sign. So let's say we have an unsigned number 129 = 1000 0001, one complement would be 1111 1110 (the inverse of all bits except the first), which is -126. Two's complement 129 would be 1111 1110 + 1 = 1111 1111 or -127.

How to check input numbers - something like this

```
var i = parseInt(document.getElementById('theInput').value);
if (i < 0 || i > 255 || isNaN(i)) { alert('Invalid input number'); return;}
```

And as stated earlier, `var twos = pad((parseInt(ones, 2) + 1).toString(2), 8);`

ps: http://jsbin.com/jahesosafana/1/edit

there is one interesting point about the number 128: -128 and 128 in Appendix 2

In two-digit notation, a non-negative number is represented by the formula as its usual binary representation; in this case, the most significant bit is 0. Although the range of numbers represented is not the same as with unsigned binary numbers. For example, an 8-bit unsigned number can represent values ββfrom 0 to 255 (11111111). However, the two's complement of an 8-bit number can only represent positive integers between 0 and 127 (01111111), since the remaining bit patterns with the most significant bits as "1" represent negative integers between -1 and -128.

source to share