简体   繁体   English

javascript sharedArrayBuffer 和按位运算返回 32 位而不是 16 位数字

[英]javascript sharedArrayBuffer and bitwise operations returning a 32bit instead of 16bit number

javascript, 2020: I've never worked with the sharedArrayBuffer, (or bits) and I have some questions. javascript,2020 年:我从未使用过 sharedArrayBuffer(或位),我有一些问题。 Basically I want to store a set of bools and a small counter (4-bit), on a single Int16Array element.基本上我想在单个 Int16Array 元素上存储一组布尔值和一个小计数器(4 位)。 but as I manipulate the slot of memory- it looks like it changes to 32-bits.但是当我操作内存插槽时 - 看起来它变成了 32 位。

  const cellbinaryMaintain = 15; //should be last bit position
   const bitArray = new SharedArrayBuffer(bufferMemoryAllocation); // double the number of cells 
   const mainGrid = new Int16Array(bitArray); // 16-bit int

and down in a for(i) loop:然后在 for(i) 循环中:

        mainGrid[i]=setBitToOne(mainGrid[i], cellbinaryMaintain);

where the 'setBit' functions are:其中“setBit”函数是:

function setBitToOne(what, bitPos) 
    {
    return what | (1 << bitPos);
}

function setBitToZero(what, bitPos)
    {
    const mask = ~(1 << bitPos);
    return what & mask;
}

but that results in:但这会导致:

decimal:-32768十进制:-32768

in binary: 11111111111111111000000000000000二进制:11111111111111111000000000000000

when the result I'm seeking is the last bit of a 16bit number set:当我寻求的结果是 16 位数字集的最后一位时:

decimal: 32768十进制:32768

in binary: 1000000000000000二进制:1000000000000000

I'm not used to working with bit-wise operators, do I need to mask out the +16bits?我不习惯使用按位运算符,是否需要屏蔽 +16 位? I've tried passing the index to the function, and work without reassigning the value to a new variable, but that still didn't work.我试过将索引传递给函数,并且在不将值重新分配给新变量的情况下工作,但这仍然不起作用。

(index, bitPos) //same 11111111111111111000000000000000
     {
     console.log(mainGrid[index], "value is");  // value is 0
     console.log(bitPos, "bit is"); // value is 15
     mainGrid[index]|=(1<<bitPos);
} 

and when I console out the mask i'm applying i of course get:当我安慰我正在应用的面具时,我当然会得到:

decimal:32768十进制:32768

binary: 1000000000000000,二进制:1000000000000000,

exactly what I want.正是我想要的。 same with the below code:与以下代码相同:

let num = 0;
let result =  (num|=1<<15);

console.log(result, "result");
bLog(result); //binary log

which seems to work... so what is happening here, why does this not seem to work with an Int16Array?这似乎有效......那么这里发生了什么,为什么这似乎不适用于 Int16Array?

The values in mainGrid are still OK - the bits that you put into it are in there, and no extras (after all there's no room for them to fit). mainGrid中的值仍然可以 - 您放入其中的位在那里,没有额外的(毕竟没有空间适合它们)。 However, the values would get printed in a funny way, because loading from an element from an Int16Array means (by definition) that the top bit is interpreted as having a value of -32768, instead of +32768 that it would have for an Uint16Array .但是,这些值会以一种有趣的方式打印出来,因为从Int16Array的元素加载意味着(根据定义)最高位被解释为具有 -32768 的值,而不是 Uint16Array 的Uint16Array . The practical consequence of that is that the value gets sign-extended when loaded (the whole Number thing in JavaScript complicates the story but not in a way that matters here).这样做的实际后果是值在加载时被符号扩展(JavaScript 中的整个Number事情使故事复杂化,但在这里并不重要)。

Using Uint16Array is less confusing, as no sign-extension occurs.使用Uint16Array不太容易混淆,因为不会发生符号扩展。 Alternatively you could manually mask out the extra bits before printing, by using:或者,您可以使用以下方法在打印前手动屏蔽掉多余的位:

value & 0xFFFF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM