xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js bitwise operators

js bitwise operators

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

https://hackernoon.com/programming-with-js-bitwise-operations-393eb0745dc4

https://dev.to/puritanic/nsfw-use-cases-for-bitwise-operators-in-js-2om5

https://stackoverflow.com/questions/654057/where-would-i-use-a-bitwise-operator-in-javascript

7 times, without multiplication


const autoSeventTimes = (num = 0, times = 7) => {
    let x = Math.floor(times / 2);
    return (num << x) - num;
};

let xyz = autoSeventTimes(3);
// 21

console.log(`xyz`, xyz);



console.log(5 && 13); // 0101 && 1101 = 1101
// expected output: 13;

console.log(parseInt("0101",2) & parseInt("1101",2));
// expected output: 5;

console.log(5 & 13 & 3); // 0101 & 1101 & 0011 = 0001
// expected output: 1;

console.log(5 | 13); // 0101 | 1101 = 1101
// expected output: 13


js benchmarks

https://jsperf.com/math-floor-vs-math-round-vs-parseint/18









<< , >>

The left-shift and right-shift operators are equivalent to multiplication and division by x * 2y respectively.

左移和右移算子分别相当于乘法和除以x * 2y。

https://muyiy.cn/question/program/102.html

https://stackoverflow.com/a/36762031/5934465

https://stackoverflow.com/questions/276706/what-are-bitwise-operators

let log = console.log;

var str = `abcedfffccbahijg`;

let s = str.match(/(\w)\1*/g);
// log(`s =`, s);
// ["a", "b", "c", "e", "d", "fff", "cc", "b", "a", "h", "i", "j", "g"]


// var x = [].concat(...([...new Array(num)].map(x => [...new Array(7)])));
// var x = [...new Array(num)].map(x => [...new Array(7)]);

// new Uint8Array(100).map((item, i) => (item = i));
// new Uint8Array(7).map(item => item = 1);

// var x = new Array(num);
// new Array(3);
// (3) [empty × 3]


let num = 3;

let old = [].concat(...([...new Array(num)].map(x => [...new Array(7)])));

console.log(old, old.length);

let result = [].concat(...([...new Uint8Array(num).map(item => item = 1)].map(x => [...new Uint8Array(7).map(item => item = 1)])));

console.log(result, result.length);
// [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
// 21


https://repl.it/@xgqfrms/

https://jscomplete.com/playground

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2019-08-15 09:11  xgqfrms  阅读(29)  评论(7编辑  收藏  举报