From 3c7952f27537f439cddb8ef3b9fe54b099f46ce5 Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Mon, 1 Jul 2019 13:20:41 +0300 Subject: [PATCH] fixes ~n --- 1-js/05-data-types/03-string/article.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/1-js/05-data-types/03-string/article.md b/1-js/05-data-types/03-string/article.md index c331ccb6..fa76f448 100644 --- a/1-js/05-data-types/03-string/article.md +++ b/1-js/05-data-types/03-string/article.md @@ -305,7 +305,8 @@ if (str.indexOf("Widget") != -1) { } ``` -````smart header="The bitwise NOT trick" +#### The bitwise NOT trick + One of the old tricks used here is the [bitwise NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_NOT) `~` operator. It converts the number to a 32-bit integer (removes the decimal part if exists) and then reverses all bits in its binary representation. For 32-bit integers the call `~n` means exactly the same as `-(n+1)` (due to IEEE-754 format). @@ -321,9 +322,9 @@ alert( ~-1 ); // 0, the same as -(-1+1) */!* ``` -As we can see, `~n` is zero only if `n == -1`. +As we can see, for 32-bit integers `~n` is zero only if `n == -1`. -So, the test `if ( ~str.indexOf("...") )` is truthy that the result of `indexOf` is not `-1`. In other words, when there is a match. +So, the test `if ( ~str.indexOf("...") )` is truthy only if the result of `indexOf` is not `-1`. In other words, when there is a match. People use it to shorten `indexOf` checks: @@ -338,7 +339,10 @@ if (~str.indexOf("Widget")) { It is usually not recommended to use language features in a non-obvious way, but this particular trick is widely used in old code, so we should understand it. Just remember: `if (~str.indexOf(...))` reads as "if found". -```` + +Technically speaking, numbers are truncated to 32 bits by `~` operator, so there exist other big numbers that give `0`, the smallest is `~4294967295=0`. That makes such check is correct only if a string is not that long. + +Right now we can see it only in the old code, as modern JavaScript provides `.includes` method (see below). ### includes, startsWith, endsWith