Last two days, I touched frequently with Bitwise Operators in Javascript
, but I did not figure out what the exactly it is and got
confused. Hence, after google many related resources, this post here to
try to introduce the Bitwise Operators and some efficient skills using in codes.
In Javascript compiler, Bitwise Operators treat their operands as a sequence of 32 bits, just zeros or ones. And the maximum and minimum integers are 2147483647 and -2147483648, which represented through a 32bit signed number.
Signed 32-bit integers
var maximum = 2147483647;
maximum.toString(2); // 1111111111111111111111111111111
var minimum = -2147483648;
minimum.toString(2); // -10000000000000000000000000000000
Notes that the maximum and minimum above in Javascript
. Things
may get strang if you exceed the scope. And if you lack the basis, go to
MDN to learn more.
Bitwise Operators
In Javascript
, the bitwise operators convert the operands to 32-bit
integers and express it a series of bits. Each bit correspond its bit, and the
result in constructed bitwise.
Bitwise Operands
As we know, Javascript
is a weak typing programming language. Actually, all values can be bitwise operand, for example, a function or a string. However, I guess, javascript
will convert the operand to int or long at first internally according to its engine. Some values like function or string which can not be correctly converted will be 0.
// 88.88 -> 88, 88 | 0 -> 88
console.log(88.88 | 0); // 88
// '88.88' -> 88, 88 | 0 -> 88
console.log('88.88' | 0); // 88
// 'sss' -> 0, 0 | 1 -> 1
console.log('sss' | 1); // 1
// [] -> 0, 0 & -1 -> 0
console.log([] & -1); // 0
Skills with Bitwise Operators
Your codes with nice usage of bitwise operators will be much more efficient. And the situation below you may meet frequently.
// x represents the number under the scope of maximum and minimum
// x & 0 will return 0
console.log(Math.random() * 2147483647 & 0); // 0
console.log(Math.random() * -2147483648 & 0); // 0
// x & -1 will return x
console.log(Math.random() * 2147483647 & -1); // random number x
console.log(Math.random() * -2147483648 & -1); // random number x
// x | 0 will return x
console.log(Math.random() * 2147483647 | 0); // random number x
console.log(Math.random() * -2147483648 | 0); // random number x
// x | -1 will return -1
console.log(Math.random() * 2147483647 | -1); // -1
console.log(Math.random() * -2147483648 | -1); // -1
// Even or Odd, Odd returns 1, Even returns 0
console.log(1111 & 1); // 1
console.log(2222 & 1); // 0
// ~x will return -(x + 1)
console.log(~(Math.random() * 2147483647)); // -(x + 1)
// x ^ 0 will return x, x ^ -1 will return ~x
console.log(Math.random() * 2147483647 ^ 0); // random number x
console.log(Math.random() * 2147483647 ^ -1); // ~x
// x << y will return x * 2 ^ y (^ is power)
console.log(8 << 4); // 128
// replace name.indexOf(subName) === 3
var name = 'Tinple';
var subName = 'ple';
if (~name.indexOf(subName)) {
// if true
} else {
// if else
}
END
Using bitwise operator correctly will enhance your code performance. And the datas in this post are tested in V8, if exist some errors, welcome to point it!