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

js bitwise operation All In One

js bitwise operation All In One

js 位运算

&

按位 AND


|

按位 OR


`^

按位异或 / XOR

let a = 5;      // 00000000000000000000000000000101
a ^= 3;         // 00000000000000000000000000000011
console.log(a); // 00000000000000000000000000000110

let b = 5;      // 00000000000000000000000000000101
b = b ^ 3;      // 00000000000000000000000000000011
console.log(b); // 00000000000000000000000000000110
// 6


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

~

按位取反/按位


>>

按位右移


<<

按位左移

const a = 5;         // 00000000000000000000000000000101
const b = 2;         // 00000000000000000000000000000010
console.log(a << b); // 00000000000000000000000000010100
// 20

const x = 5;         // 00000000000000000000000000000101
const y = 3;         // 00000000000000000000000000000011
console.log(x << y); // 00000000000000000000000000101000
// 40
(5 << 3).toString(2)
// "101000"
parseInt("101000", 2)
// 40

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

refs

位运算(&、|、^、~、>>、<<)

https://www.runoob.com/w3cnote/bit-operation.html

js 进制转换

https://www.cnblogs.com/xgqfrms/p/13532592.html

^ 异或

https://www.cnblogs.com/xgqfrms/p/13526984.html



©xgqfrms 2012-2020

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

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


posted @ 2020-08-20 10:25  xgqfrms  阅读(114)  评论(1编辑  收藏  举报