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
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
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-02-21 vue 如何在父组件中获取 slot 中的子组件 props All In One
2022-02-21 css parent color depend child status All In One
2021-02-21 Vue filter All In One
2021-02-21 Vue 3.x Composition API All In One
2021-02-21 Vue mixin All In One
2021-02-21 Vite 2.x All In One
2021-02-21 js tada or confetti animation effect All In One