How to write negative binary number? [duplicate] - javascript

This question already has an answer here:
ECMAScript 6 negative binary literal
(1 answer)
Closed 3 years ago.
So 0b1 is 1. How to write -1 in binary format?
The docs says that the leftmost bit is reserved for sign, but where is that leftmost bit? Even 0b11111111111111111111111111111111 is still a positive number. I naively tried 1b with no success of course.

try
let a = -0b1;
console.log(a);

Related

Why does a JavaScript function automatically convert a binary number when I pass it as an argument? [duplicate]

This question already has answers here:
Number with leading zero in JavaScript
(3 answers)
Closed 3 years ago.
I guess I can format it back. I'm just interested in why it's happening.
function test(d){
console.log(d) // 151028224
}
console.log(test(00001100101000))
By default, any number literally written with a zero at the beginning is considered as octal (base 8) number representation, and then, when you show back any number with console.log, it is written as its base 10 representation.
console.log(05)
console.log(06)
console.log(07)
console.log(010)
console.log(011)
It's recommended to avoid this in code, because it can lead to confusions :
if the number contains the digits 8 or 9, it cannot be a base-8 number, and thus treated as base 10 !
console.log(05)
console.log(06)
console.log(07)
console.log(08) // Yiiik !
console.log(09) // Yiiik !
console.log(010)
console.log(011)
The function has nothing to do with it.
The JavaScript compiler converts your number literal into a Number when it compiles the source code.
Since the number starts with a 0, it is treated as octal instead of decimal.

What does `>>>` mean in JavaScript? [duplicate]

This question already has answers here:
What is the JavaScript >>> operator and how do you use it?
(7 answers)
Closed 5 years ago.
What is the meaning of the expression >>> in JavaScript? It is like type conversion, or what, and when it recommended to use?
I ran into that symbol (>>>) when I read this article and am a little confused.
Sorry, if my question is stupid, but I can not find any answers by Google search or other ways.
>>> is a bitwise operator.
>>> (Zero-fill right shift) This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the
right are discarded. Zero bits are shifted in from the left. The sign
bit becomes 0, so the result is always non-negative.
For non-negative numbers, zero-fill right shift and sign-propagating
right shift yield the same result. For example, 9 >>> 2 yields 2, the
same as 9 >> 2
From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

How does Javascript get this value? [duplicate]

This question already has answers here:
Why JavaScript treats a number as octal if it has a leading zero
(3 answers)
Closed 5 years ago.
We're having a discussion in the office about how the hell this math works in JavaScript.
There was an instance where we were multiplying by 010 instead of 10 and this gave the incorrect returned value.
For example...
25.25 * 010 = 202
25.25 * 10 = 252.5 as expected
whats even weirder is if you do parseFloat(010) it gives you 8!
For 010 is decimal 8, so it get 202.
console.log(25.25 * 010);
Look at this answer for Java: Why "010" equals 8?. JavaScript has to do the same.
The answer is, that an octal number starts with a leading zero 0.

Converting Decimal Number to Binary [duplicate]

This question already has answers here:
What is the JavaScript >>> operator and how do you use it?
(7 answers)
How do I convert an integer to binary in JavaScript?
(17 answers)
Closed 5 years ago.
I found this code on W3schools for converting decimal into binary.
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
I don't understand what is the purpose for making zero fill right shift in this case? What is the use of setting the shift to 0 (i.e. dec >>> 0) since as I know it doesn't push any binary digit to the right at all?
I am a JS beginner. Thanks!

ParseInt 16 radix number to 10 radix [duplicate]

This question already has an answer here:
JavaScript parseInt is giving me wrong number, what I'm doing wrong? [duplicate]
(1 answer)
Closed 7 years ago.
Why?
parseInt((10152174800132377).toString(16),16) ==
10152174800132376
Where the number of lost?
The number is lost, as it is to large to be representable exactly in JavaScript's Number type (it is a IEEE 754 double):
$ node
> 10152174800132377
10152174800132376

Categories