首页 > 解决方案 > Node.js 按位 OR 停止为大数字工作

问题描述

我正在尝试将 0 到 15 的 16 s 数组“压缩”number为单个number.

由于数组的每个元素最多为 15,我可以只用 4 位来表示它,所以我希望有 4 位 * 16 = 64 位适合number.

为了做这个“压缩”,我使用了休闲功能:

function compressArray(array) {
  return array.reduce(
    (pre, curr, index) => {
      return (pre | (curr * Math.pow(2, index * 4)));
    },
    0
  );
}

但是在索引 8 之后,它会继续给我相同的输出而不计算正确的|.

我在这里想念什么?

标签: node.jsintegercompressionlogical-operators

解决方案


推荐阅读