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 @   xgqfrms  阅读(30)  评论(7编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-08-15 钉钉-开放平台 & 微信 share
2016-08-15 面试秘籍:如何在面试前判断一家公司靠不靠谱? All In One
点击右上角即可分享
微信分享提示