js double bitwise not ~~ All In One
js double bitwise not ~~ All In One
js 双非位运算
~~
0
vs NaN
const arr = [];
// []
~~arr.pop();
// 0
+arr.pop();
// NaN
const arr = [];
// []
const a = arr.pop();
// undefined
~~a;
// 0
+a;
// NaN
demos
const addStrings = function(str1, str2) {
let res = '';
let temp = 0;
const arr1 = str1.split('');
const arr2 = str2.split('');
while (arr1.length || arr2.length || temp) {
// string => number ✅
let a = ~~arr1.pop();
let b = ~~arr2.pop();
console.log(`a, b`, a, b);
temp += a + b;
console.log(`temp`, temp);
// 字符串相加,要注意先后顺序
res = (temp % 10) + res;
// 进位
temp = temp > 9 ? 1 : 0;
}
// 正则表达式去除头部的 0
return res === "0" ? res : res.replace(/^0+/, '');
}
/*
addStrings([1,0,0,0,0,1].join(``), [4, 5, 6].join(``));
a, b 1 6
temp 7
a, b 0 5
temp 5
a, b 0 4
temp 4
a, b 0 0
temp 0
a, b 0 0
temp 0
a, b 1 0
temp 1
'100457'
*/
const addStrings = function(str1, str2) {
let res = '';
let temp = 0;
const arr1 = str1.split('');
const arr2 = str2.split('');
while (arr1.length || arr2.length || temp) {
// string => number ❌ NaN bug
let a = +arr1.pop();
let b = +arr2.pop();
console.log(`a, b`, a, b);
temp += a + b;
console.log(`temp`, temp);
// 字符串相加,要注意先后顺序
res = (temp % 10) + res;
// 进位
temp = temp > 9 ? 1 : 0;
}
// 正则表达式去除头部的 0
return res === "0" ? res : res.replace(/^0+/, '');
}
addStrings([1,0,0,0,0,1].join(``), [4, 5, 6].join(``));
/*
a, b 1 6
temp 7
a, b 0 5
temp 5
a, b 0 4
temp 4
a, b 0 NaN
temp NaN
a, b 0 NaN
temp NaN
a, b 1 NaN
temp NaN
'NaNNaNNaN457'
*/
bugs
❌ NaN bug
+undefined
=>NaN
const addStrings = function(str1, str2) {
let res = '';
let temp = 0;
const arr1 = str1.split('');
const arr2 = str2.split('');
while (arr1.length || arr2.length || temp) {
// string => number ❌ NaN bug
temp += +arr1.pop() + +arr2.pop();
console.log(`temp`, temp);
// 字符串相加,要注意先后顺序
res = (temp % 10) + res;
// 进位
temp = temp > 9 ? 1 : 0;
}
// 正则表达式去除头部的 0
return res === "0" ? res : res.replace(/^0+/, '');
}
addStrings([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1].join(``), [4, 5, 6].join(``));
/*
temp 7
temp 5
temp 4
28 x temp NaN
'NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN457'
*/
solutions
✅
~~undefined
=>0
const addStrings = function(str1, str2) {
let res = '';
let temp = 0;
const arr1 = str1.split('');
const arr2 = str2.split('');
while (arr1.length || arr2.length || temp) {
// string => number ✅
// ~~ double bitwise not / 双非位运算
temp += ~~arr1.pop() + ~~arr2.pop();
console.log(`temp`, temp);
// 字符串相加,要注意先后顺序
res = (temp % 10) + res;
// 进位
temp = temp > 9 ? 1 : 0;
}
// 正则表达式去除头部的 0
return res === "0" ? res : res.replace(/^0+/, '');
}
addStrings([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1].join(``), [4, 5, 6].join(``));
/*
2 x temp 6
temp 4
27 x temp 0
temp 1
'1000000000000000000000000000466'
*/
避免
+undefined
=>NaN
✅
// 字符串相加 / 大数相加
const addStrings = function(str1, str2) {
let res = '';
let temp = 0;
const arr1 = str1.split('');
const arr2 = str2.split('');
while (arr1.length || arr2.length || temp) {
// ~~ double bitwise not / 双非位运算 ✅
// temp += ~~arr1.pop() + ~~arr2.pop();
// string => number ✅
if(arr1.length && arr2.length) {
let a = +arr1.pop()
let b = +arr2.pop();
temp += a + b;
} else {
if(arr1.length) {
let a = +arr1.pop()
temp += a;
}
if(arr2.length) {
let b = +arr2.pop();
temp += b;
}
}
// string => number ❌
// temp += +arr1.pop() + +arr2.pop();
// 字符串相加,要注意先后顺序
res = (temp % 10) + res;
// 进位
temp = temp > 9 ? 1 : 0;
}
// console.log('res =', res, typeof res);
return res === "0" ? res : res.replace(/^0+/, '');
}
js 大数相加,科学计数法 bug
const a = parseInt([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1].join(``));
const b = parseInt([4, 6, 5].join(``));
const sum = a + b;
// 1e+30 ❌
// wanted ✅
// 1000000000000000000000000000466
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
BigInt
digits = [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]
// (19) [6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 5, 5, 4, 3]
BigInt(digits.join(''))
// 6145390195186705543n
BigInt(digits.join('')).toString().split("").map(Number)
// (19) [6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 5, 5, 4, 3]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
Number.MAX_SAFE_INTEGER
The Number.MAX_SAFE_INTEGER
static data property represents the maximum safe integer
in JavaScript (2**53 – 1
).
Number.MAX_SAFE_INTEGER 静态数据
属性表示 JavaScript 中的最大安全整数
(2**53 – 1
)。
Number.MAX_SAFE_INTEGER
9007199254740991
2 ** 64 - 1
18446744073709552000
2 ** 32 - 1
4294967295
refs
https://www.cnblogs.com/xgqfrms/p/18230786
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17139557.html
未经授权禁止转载,违者必究!