Asked  7 Months ago    Answers:  5   Viewed   24 times
var attr = ~'input,textarea'.indexOf( target.tagName.toLowerCase() )
           ? 'value'
           : 'innerHTML'

I saw it in an answer, and I've never seen it before.

What does it mean?



~ is a bitwise operator that flips all bits in its operand.

For example, if your number was 1, its binary representation of the IEEE 754 float (how JavaScript treats numbers) would be...

0011 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

So ~ converts its operand to a 32 bit integer (bitwise operators in JavaScript do that)...

0000 0000 0000 0000 0000 0000 0000 0001

If it were a negative number, it'd be stored in 2's complement: invert all bits and add 1.

...and then flips all its bits...

1111 1111 1111 1111 1111 1111 1111 1110

So what is the use of it, then? When might one ever use it?

It has a quite a few uses. If you're writing low level stuff, it's handy. If you profiled your application and found a bottleneck, it could be made more performant by using bitwise tricks (as one possible tool in a much bigger bag).

It's also a (generally) unclear trick to turn indexOf()'s found return value into truthy (while making not found as falsy) and people often use it for its side effect of truncating numbers to 32 bits (and dropping its decimal place by doubling it, effectively the same as Math.floor() for positive numbers).

I say unclear because it's not immediately obvious what it is being used for. Generally, you want your code to communicate clearly to other people reading it. While using ~ may look cool, it's generally too clever for its own good. :)

It's also less relevant now that JavaScript has Array.prototype.includes() and String.prototype.includes(). These return a boolean value. If your target platform(s) support it, you should prefer this for testing for the existence of a value in a string or array.

Tuesday, June 1, 2021
answered 7 Months ago

It is just a shorthand for "same as in declaration".

Some IDEs, e.g. IntelliJ use this too.

The files on disk do not have this notation, which is only a compaction in the IDE GUI.

Monday, June 28, 2021
answered 6 Months ago

According to the ES5 spec, when doing bitwise operations, all operands are converted to ToInt32 (which first calls ToNumber. If the value is NaN or Infinity, it's converted to 0).

Thus: NaN ^ 1 => 0 XOR 1 => 1

Thursday, August 5, 2021
answered 5 Months ago

The ; separates the two commands.

echo a; echo b

It lets the bash know that echo a and echo b are two separate commands and need to be run separately one after the other

Try without semicolons

$ echo a echo b
a echo b

Here the statement is taken as a single command echo and a echo b is passed as parameter to the echo

Thursday, August 5, 2021
answered 5 Months ago

Let's say you were programming something to mimic a piece of hardware, specifically a shift register.

To make things easier I'll use 8 bits instead of 32 bits as in your question.

We can add 128 every time we want to feed a high bit into this shift-register, since it will make the leftmost bit 1.

// Assume n is initialised to 0, so n = 00000000 in binary
n += 128;                    // now n = 10000000 in binary

If we shift using >>> every time you want to simulate a clock cycle, then after 8 "clock cycles" we will have that 1 at the rightmost bit. If we read that rightmost bit out then we will get a delayed version of what was fed into the leftmost bit 8 cycles ago.

This is only one example where the bits are not interpreted as a number, and I am sure there are many more. I think you'll find a few more uses elsewhere, especially in software meant to mimic hardware circuits/building blocks.

Thursday, August 12, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :