Binary sum result

Asked

Viewed 304 times

4

A program capable of operating two binary numbers between 0-255. Program input needs to be in binary and output as well.

I did it in a way that was working, so that if I pass in binary 1 + 11, the result comes out right. However, if I pass 00000001 + 00000011, the result is NaN.\

Code:

function op(s, v1, v2) {
    const operator = s.toString()
    const valor1 = v1.toString()//Passei os valores para String
    const valor2 = v2.toString()

    const v1Number = parseInt(valor1, 2)//Passei de string para Number em decimal
    const v2Number = parseInt(valor2, 2)

    console.log('aqui', v1Number);
    console.log('aqui', v2Number);

    var result = 0
    switch (operator) {
        case '+':
            result = v1Number + v2Number
            console.log(result.toString(2))
            break;

        case '-':
            result = v1Number - v2Number
            console.log(result.toString(2))
            break;

        case '*':
            result = v1Number * v2Number
            console.log(result.toString(2))
            break;

        case '/':
            result = v1Number / v2Number
            console.log(result.toString(2))
            break;

        case '%':
            result = v1Number % v2Number
            console.log(result.toString(2))
            break;
    }
}

What is going on:

op('+', 1, 11) // Funciona.

op('+', 00000001, 00000011) // Retorna `NaN`.
  • 1

    Related: https://answall.com/questions/117657/n%C3%bameros-com-inicio-zero-em-javascript

  • Binary-Calculator in https://codepen.io/bdemaloney/pen/MyKjov

  • https://ayidouble.github.io/Binary-Calculator-JavaScript/

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

6

The concept of the question is all wrong, there is no binary number, number is number, arithmetic is arithmetic, binary or decimal is just the way to present on screen or paper, it is not accounted for. The same goes for data entry, if you’re going to enter a format like this, you have to do it as text, or the data is a string or he is the text of the code.

If you write 3 in your code it is a text in the code that we know it is equivalent to 3 units. If you write 0b00000011, is also writing an equal number, also what a human understands as 3 units. And the computer also understands how 3, no matter how the text was written. In fact we can represent a number of various forms that are not always text, here is another way to say that has three (opa, here is another way to put in text):

3 patos de borracha

If you add the count in this image, or 3 with another number, no matter in what form it was represented, the result will happen the same.

Never confuse textual representation with number, you do not count with textual representation, only represents a number, and so can use several notations.

Look at the mess of the problem:

A program capable of operating two binary numbers between 0-255

First, there are no binary numbers, there are numbers that have been represented with binary notation. Numbers only represent a count of something. And interestingly the text uses decimal notation to tell which numbers are possible.

If you will receive the data as text then you need to do a conversion, and Javascript already has a function that does this, as you already know and used correctly. Then first convert the text to number and then do the math. Note that the account is not done in a special way because you received a text to represent binarially, you convert the text to number, and then the bill is normal.

At the end, print the textual representation in binary notation of the result number. If you print without making the explicit conversion the JS will do a conversion to text implicitly, because it can only print a text that represents the number, and the default of the JS is to do this in decimal notation. If he were to print the number directly without turning it into text it would appear on the screen something incomprehensible by a human.

The code gets weird doing meaningless conversions, convert only what is needed. It can be much simpler:

function op(s, v1, v2) {
    const v1Number = parseInt(v1, 2);
    const v2Number = parseInt(v2, 2);
    var result = 0;
    switch (s) {
    case '+':
        result = v1Number + v2Number;
        break;
    case '-':
        result = v1Number - v2Number;
        break;
    case '*':
        result = v1Number * v2Number;
        break;
    case '/':
        result = v1Number / v2Number;
        break;
    case '%':
        result = v1Number % v2Number;
        break;
    }
    console.log(result.toString(2));
}

op('+', '00000001', '00000011');

I put in the Github for future reference.

Another way is to use binary notation in the code but not to use the data as binary, treat only as number (understand that it is only giving the textual representation in binary, but the compiler itself will convert to number what was written in text in decimal notation - the normal compiler is to do this with decimal notation, so make no mistake, what you write in your code is just a text, it’s not a number, you know it will turn number in the compilation, unlike the code above that the conversion is done in running):

function op(s, v1Number, v2Number) {
    var result = 0;
    switch (s) {
    case '+':
        result = v1Number + v2Number;
        break;
    case '-':
        result = v1Number - v2Number;
        break;
    case '*':
        result = v1Number * v2Number;
        break;
    case '/':
        result = v1Number / v2Number;
        break;
    case '%':
        result = v1Number % v2Number;
        break;
    }
    console.log(result.toString(2));
}

op('+', 0b00000001, 0b00000011);
op('+', 1, 3); //note que funciona igual

I put in the Github for future reference.

I’m going to shoot rockets the day people understand which number is number, it’s different than a text that shows what that number should be. Decimal, binary or hexadecimal notation are not numbers, they are texts of how you write the number. So be careful, lots of exercise, article, or answers you’ll find out there is given by those who didn’t want to understand this concept correctly and will make you understand wrong too, this myth needs to end.

See more in:

  • Thanks for the help!

  • @Albertobelo see on [tour] the best way to say thank you.

  • @Albertobelo, the best way to thank you is to mark the answer as accepted, see https://i.stack.Imgur.com/evLUR.png

  • @Albertobelo If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.