~ in javascript [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to: The ~ operator?
What's the ~ operator does in javascript?
input
alert(~1)
output is
-2
input
~function () {}()
output is
-1
I never heard about ~ operator in javascript

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
Bitwise NOT ~ a Inverts the bits of its operand.
I guess its fairly odd that a function returns -1, but what would you expect anyway.

This is the bitwise not operator that inverts the value of every bit in the integer. In binary a signed integer has the following representation:
00000001 = 1
11111110 = -2
See this wikipedia article.

The bitwise NOT operator (~) will take its operand, convert it to a
32-bit integer, and will invert each bit so that each 0 becomes a 1
and vice versa.
http://james.padolsey.com/javascript/double-bitwise-not/

Related

Why does the bitwise operator OR truncate the value in this example? [duplicate]

This question already has answers here:
Why does OR 0 round numbers in Javascript?
(3 answers)
Closed 5 years ago.
The Mozilla JavaScript Guide has this to say about the bitwise OR operator:
"Bitwise OR: Returns a zero in each bit position for which the corresponding bits of both operands are zeros."
However, when operand b is 1, the bitwise operator OR in the example below rounds up a. This is something I cannot wrap my head around. Also, seeing as a returns a non rounded number, it is also not clear to me how the bitwise operator truncates it down to two digits.
var a = Math.random()*100;
console.log(a);
console.log(a | 1);
Insightful explanations are very welcome.
The hidden factor here is bitwise operators cast the number to an integer before being applied. That is why the number gets rounded. The cast truncates any fractional part.
The bitwise OR itself then simply sets the first bit to 1. So may or may not increase the truncated number by 1 depending on whether it was odd or even after the truncation.
So it's not rounding it up in all cases - just 50% of the time.

What is ~~ in JavaScript? [duplicate]

This question already has answers here:
What does ~~ ("double tilde") do in Javascript?
(12 answers)
Closed 7 years ago.
I was just messing with random stuff, while I found something interesting..
if I have ~ before a number, for example I have tried
~110100100 // result will be " -110100101 "
~11 // result will be " -12 "
is it making it negative and reducing it by 1? I don't have any idea, can anyone pleas explain this??
The operator ~ returns that result:
~N = -(N+1)
But this is an effect of inverting the value of all bits of a variable.
Double tilde ~~ is used to convert some types to int, since ~ operator converts the value to a 32-bit int before inverting its bits. Thus:
~~'-1' = -1
~~true = 1
~~false = 0
~~5.6 = 5

what means "return+" in javascript [duplicate]

This question already has an answer here:
Single plus operator in javascript [duplicate]
(1 answer)
Closed 8 years ago.
Found some code which implements the Date.now function for older browsers, code is
Date.now=Date.now||function(){return+(new Date)};
what does the + operator do ? cant find anything on the net
From the doc:
Unary plus (+)
The unary plus operator precedes its operand and evaluates to its
operand but attempts to converts it into a number, if it isn't
already. Although unary negation (-) also can convert non-numbers,
unary plus is the fastest and preferred way of converting something
into a number, because it does not perform any other operations on the
number. It can convert string representations of integers and floats,
as well as the non-string values true, false, and null. Integers in
both decimal and hexadecimal ("0x"-prefixed) formats are supported.
Negative numbers are supported (though not for hex). If it cannot
parse a particular value, it will evaluate to NaN.
Syntax
Operator: +x
Examples
+3 // 3
+"3" // 3
+true // 1
+false // 0
+null // 0
References
Arithmetic operators
You are converting the Date object into an integer. It represents the number of miliseconds since 1/1/1970

How does the expression "float | 0" produces an integer? [duplicate]

This question already has answers here:
Using bitwise OR 0 to floor a number
(7 answers)
Closed 8 years ago.
Please describe how does 5.55 | 0 produces 5 in JavaScript. I want to know what is happening in this bitwise operating. Thanks!
The bitwise operators in Javascript automatically coerce their arguments to 32-bit integer values by dropping the fraction and any high-order bits beyond 32. So
5.55 | 0
is treated like:
5 | 0
The operands of bitwise operations are always converted to signed 32-bit integers in big-endian order and in two's complement format.
That would be
00000000000000000000000000000101
or 00000000000000000000000000000000
------------------------------------
00000000000000000000000000000101

What does the caret symbol (^) do in JavaScript? [duplicate]

This question already has answers here:
What does the ^ (caret) symbol do in JavaScript?
(5 answers)
Closed 6 years ago.
I thought that Math.pow(2,2) was equal to 2^2 but it is not. So what does ^ (caret) mean in JavaScript?
I was executing some tests in the console but didn't recognize the results:
2 ^ 2 = 0
2 ^ 3 = 1
1 ^ 2 = 3
It means bitwise XOR.
EDIT: Fixed link
It's a bitwise integer XOR operation (MDC link).
The ^ operator is bitwise XOR, you have more information in MDN:
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
That operator performs the logical XOR operation. (out bit is 1 when both input bits are different).
This is the bitwise XOR operator, which returns a one for each position where one (not both) of the corresponding bits of its operands is a one. The next example returns 4 (0100):
Code:
result = a ^ b;

Categories