javascript - 如何获取 32 位整数的位长度

标签 javascript integer bit-manipulation

我尝试了 JavaScript 的几种变体,但没有一个获得所需的结果。

assert(countIntegerBits(4), 3) // 100
assert(countIntegerBits(8), 4) // 1000
assert(countIntegerBits(20), 5) // 10100
assert(countIntegerBits(100), 7) // 1100100

// https://stackoverflow.com/questions/43122082/efficiently-count-the-number-of-bits-in-an-integer-in-javascript
function countIntegerBits(integer) {
  var length = 0

  while (integer = Math.floor(integer)) {
    if (integer & 1) {
      length++
    }

    integer /= 2
  }

  return length
}

function countIntegerBits(integer) {
  // var length = 0
  // while (integer !== 0) {
  //   length += countIntegerBits32(integer | 0)
  //   integer /= 0x100000000
  // }
  // return length
  //
  // or perhaps this:
  // https://gist.github.com/everget/320499f197bc27901b90847bf9159164#counting-bits-in-a-32-bit-integer
}

function countIntegerBits32(integer) {
  integer = integer - ((integer >> 1) & 0x55555555)
  integer = (integer & 0x33333333) + ((integer >> 2) & 0x33333333)
  return ((integer + (integer >> 4) & 0xF0F0F0F) * 0x1010101) >> 24
}

function countStringBits(string) {
  // looks like this / 8 would be good enough
  // https://codereview.stackexchange.com/questions/37512/count-byte-length-of-string
  var length = 0;
  for (var i = 0; i < normal_val.length; i++) {
    var c = normal_val.charCodeAt(i);
    length += c < (1 <<  7) ? 1 :
               c < (1 << 11) ? 2 :
               c < (1 << 16) ? 3 :
               c < (1 << 21) ? 4 :
               c < (1 << 26) ? 5 :
               c < (1 << 31) ? 6 : Number.NaN
  }
  return length;
}

function countFloatBits(float) {
  // looks too complicated for an SO question
  // http://binary-system.base-conversion.ro/real-number-converted-from-decimal-system-to-32bit-single-precision-IEEE754-binary-floating-point.php?decimal_number_base_ten=1.23&sign=0&exponent=01111111&mantissa=00111010111000010100011
}

function assert(a, b) {
  if (a !== b) throw new Error(a + ' != ' + b)
}

我想避免的是这种转换为字符串的黑客

var length = integer.toString(2).split('').length

我能想到做的唯一一件事是 check if bit is set ,直到到达第一个 1,然后从那里开始计数。

assert(countIntegerBits(4), 3) // 100
assert(countIntegerBits(8), 4) // 1000
assert(countIntegerBits(20), 5) // 10100
assert(countIntegerBits(100), 7) // 1100100

function countIntegerBits(integer) {
  var i = 0
  while (true) {
    if (integer & (1 << i)) {
      return 31 - i
    }

    i++
  }
}

function assert(a, b) {
  if (a !== b) throw new Error(a + ' != ' + b)
}

但这似乎不太正确,因为我不确定所有整数是否在底层都表示为 32 位,例如 (4).toString(2) 给出 “100”,而不是00000000000000000000000000000100,所以不确定。

在那里,我探索了如何以位为单位检查字符串的长度,与 float 相同,但是如果字符串是 utf-8 编码,则看起来很简单,但 float 似乎是一件大事,所以我的问题是仅限于 JavaScript 支持的最大整数。目前,出于所有实际目的,我将只考虑最多大约 10 亿的整数,因此不需要考虑 123e456 bigints 或任何东西,只需考虑最多几十亿的数字,或 JavaScript 中基本的 32 位整数 Max。

最佳答案

自然对数(好吧,对数到任何底数)和对数到另一个底数之间存在关系。获取以 2 为底的日志:

const log2 = n => Math.log(n) / Math.log(2);

您想在加 1 后向下舍入:

const bits = n => Math.floor(log2(n) + 1);

关于javascript - 如何获取 32 位整数的位长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54728560/

相关文章:

javascript - Jquery 分离并重新附加到另一个

javascript - 如何根据选择值追加表行?

python - 在 Python 中的字符串末尾添加一个整数

bit-manipulation - 如何在 Dart 中进行按位无符号(零填充)右移?

c++ - C++ 中循环移位(旋转)操作的最佳实践

javascript - 在 AngularJS 数据表中使用滚动按需加载数据

javascript - 为 Highcharts yAxis.Max 使用变量

c - 在 C 中这个 int 到 float 的转换有什么问题?

java - 如何使用 python 通过套接字将整数发送到 Java 应用程序?

c - C 中的 Bit Hack 看不懂