如何使用bitmasks在JavaScript中存储布尔值

问题描述 投票:2回答:3

在阅读有关如何使用bitmasks存储布尔值后,我有点困惑。我想有一组布尔值,然后为每个值组合生成一个唯一的整数。以下是目前的系统:

var a = 1 << 1
var b = 1 << 2
var c = 1 << 3
var d = 1 << 4

var uniqueint1 = getInt(true, false, false)
var uniqueint2 = getInt(true, true, false)
var uniqueint3 = getInt(true, true, true)
// ...

function getInt(x, y, z) {
  var value = a
  if (x) value = value | b
  if (y) value = value | c
  if (z) value = value | d
  return value
}

但问题是,我不确定我是否应该按照以下方式处理“不”的情况:

function getInt(x, y, z) {
  var value = a
  if (x) value = value | b
  else value = value ^ b

  if (y) value = value | c
  else value = value ^ c

  if (z) value = value | d
  else value = value ^ z

  return value
}

我所知道的是,我已经看到了与bitmasks相关的|&^,我知道要找到一个布局是否在你做var yes = value & b的位掩码中,但我只是混淆了如何生成位掩码以便它处理if(true)和if(false)两种情况。我不确定我是否应该在某个地方使用^,或者我还有什么问题。请指教。谢谢!

javascript bitmask
3个回答
5
投票

我不确定我是否应该处理“不”的情况

你根本不应该处理它。位掩码的工作原理是,如果设置了标志,则要么具有值1,要么具有值0的位(如果不是)。鉴于您的值始于0s无处不在(整数0),您只需在条件为真时设置位(通过OR 1),否则您不必执行任何操作。

您可以简化代码

function getInt(x, y, z) {
  return (0b10 // a
          | (x << 2)   // 0b00100 if x and 0b00000 else
          | (y << 3)   // 0b01000 if y and 0b00000 else
          | (z << 4)); // 0b10000 if z and 0b00000 else
}

1
投票

NOT op将反转该位,因此需要将其设置为清零。

(假设您想在现有值上打开或关闭位:)您可以将NOT与AND掩码一起使用以清除这样的位:

var v = 0xff;            // value
var bit3 = 1<<3;         // the bit we want to clear

// clear bit 3:
v &= ~bit3;              // create a AND mask inverting bit 3 and AND with value
console.log(v.toString(2));

v &= ~bit3;              // will not set the bit even when already cleared
console.log(v.toString(2));

您当然也可以检查该位是否已设置,然后检查该位是否为:

var v = 0xff;
var bit3 = 1<<3;

if (v & bit3) v ^= bit3;   // NOT bit 3 if set
console.log(v.toString(2));

// should not trigger:
if (v & bit3) console.log("Whaa");

要改为设置它,无论状态如何都使用OR:

if (v & bit3) v ^= bit3;   // NOT bit 3 if set
else {v |= bit3};          // set it not set already

如果你需要一次清除几个位,你可以先将这些位组合在一起,然后将其与NOT一起用于AND掩码:

var v = 0xff;
var bit1 = 1<<1;
var bit3 = 1<<3;

// create NOT mask:
var mask = bit1 | bit3;

// clear bits:
v &= ~mask;
console.log(v.toString(2));

所以在函数中你可以这样:

var a = 0xff;
var b = 1 << 2
var c = 1 << 3
var d = 1 << 4

function getInt(x, y, z) {
  var value = a;
  value = x ? value | b : value & ~b;
  value = y ? value | c : value & ~c;
  value = z ? value | d : value & ~d;
  return value
}

// turn off bit c/d (bit index 3 & 4):
console.log(getInt(true,false,false).toString(2));

// turn on c, off b/d
console.log(getInt(false,true,false).toString(2));

// with same value turn off all, then on c:
a = getInt(false, false, false);
console.log(getInt(false,true,false).toString(2));

0
投票

通过在开头做var value=1你已经将值设置为2,因为1<<1=2。我建议你设置value=0,并删除else value = value ^ c,因为当你以全零开始时,该操作不会引起任何变化。您的代码如下所示:

var a = 1 << 1
var b = 1 << 2
var c = 1 << 3
var d = 1 << 4

function getInt(x, y, z) {
  var value = 0
  if (x) value = value | b
  if (y) value = value | c
  if (z) value = value | d
  return value
}

var uniqueint1 = getInt(true, false, false)
var uniqueint2 = getInt(true, true, false)
var uniqueint3 = getInt(true, true, true)
uniqueint1

这打印出4,而不是6uniqueint1

© www.soinside.com 2019 - 2024. All rights reserved.