首页 > 其他分享 >js double bitwise not ~~ All In One

js double bitwise not ~~ All In One

时间:2023-02-21 02:44:06浏览次数:30  
标签:const temp double NaN bitwise js let arr1 res

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

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'

*/

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 ✅
    // ~~ 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'
*/

image

❌ NaN bug +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 ❌  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'
*/

!image

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

(

标签:const,temp,double,NaN,bitwise,js,let,arr1,res
From: https://www.cnblogs.com/xgqfrms/p/17139557.html

相关文章

  • js---弹框提示和确认提示
    最近在开发PC端项目的时候,涉及到前后端交互,需要有一个弹框提示和一个确认弹框的提示,当然使用浏览器的alert和confirm就能够解决这个问题,但是这个的样式太丑了,不好看,考......
  • spring中处理json
    1.使用fastJson2.controller层@PostMapping("")@ResponseBodypublicBaseResponsejsonFunction(@RequestBodyStringjsonData){BaseResponsebaseResponse=ne......
  • mapToDouble.sum的精度丢失问题
    需要四舍五入一下packagecn.***;importjava.math.RoundingMode;importjava.text.DecimalFormat;/***小数位获取*/publicclassDecimalAcquisition{publicst......
  • 第一章、nodejs高级
    目录一、nodejs基础1、认识nodejs2、nodejs的特性3、使用nodejs需要了解多少javascript4、浏览器环境vsnode环境5、开发环境搭建二、模块、包、commonjs1、为什么要有模块......
  • Vue 3 兄弟组件间传值 | mitt.js
    Vue3中兄弟间传值可以使用Vuex,但小项目使用过于庞大,我们可以使用mitt进行兄弟组件间传值。操作步骤第一步:安装mittnpmimitt第二步:创建文件(例如:eventBus.js)impo......
  • jsp页面交互
    1、什么是内置对象?JSP的内置对象是指在JSP页面系统中已经默认内置的Java对象,这些对象不需要开发人员显式声明即可使用。在JSP页面中,可以通过存取JSP内置对象实现与JSP页......
  • 用jquery.form.js 实现ajax提交含有上传文件和普通字段的表单
    前两天用到ajax提交含上传图片的表单。实现的功能比较简单,但是使用ajax提交表单form.serialize只能实现普通的字段提交,上传图片却不能使用。网上很多都是推荐使用jquer......
  • Node.JS 流式文件的读取
    视频07.流式文件读取.js08.流式文件读取.js......
  • js获取blob数据流中携带的msg消息
    问题:后端返回的失败blob数据流,前端如何捕获展示  业务需求,需要后端控制文件下载频率,用户在短时间内不可以重复下载文件,并返回消息提示(剩余XX秒)。前端需要把返......
  • JSP大文件上传解决方案
    ​ 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现。先说下要求:PC端全平台支持,要求支持Windows,Mac,Linux......