E
E
edu3782015-10-14 06:39:57
JavaScript
edu378, 2015-10-14 06:39:57

Reason for bitwise result NOT in JavaScript?

There is a number 9 (base 10) = 0000 0000 0000 0000 0000 0000 0000 1001 (base
2
)
is this number equal to -10 in decimal? The sign "-" is clear, the unit occupies one of 32 bits. But the rest of the translation doesn't translate to 10. When using result.toString, the browser returns -1010. Explain, please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lynn "Coffee Man", 2015-10-14
@edu378

https://ru.m.wikipedia.org/wiki/%D0%94%D0%BE%D0%BF...
Aves also suggested https://learn.javascript.ru/bitwise-operators#sign...

A
Anton Fedoryan, 2015-10-14
@AnnTHony

What did you hope or expect this number to be?
If the most significant digit is "1", then the number is negative.
How to get negative in decimal from binary code? Just!
To do this, you need to understand how to get negative in binary. What we have: bit depth - 32 bits, the number that needs to be converted to binary code "-10".
In hexadecimal code it will be like this: (0xFFFFFFFF - 0xA) + 0x1, this is the same as representing the number "+10" in the reverse code and adding one to the result.
You can also do this: 4294967295 - 10 + 1, which in binary code will give the same result 1111 1111 1111 1111 1111 1111 1111 0110
To get a positive number, the operation is the same, the reverse code is + 1.
I don’t know JS well, but the result is logical: he translated 1010 as 10 in decimal ss and simply signed the minus sign.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question