我希望在键入的16 16Uint数组项中使用值为2^4-2^8的位作为16的二进制计数器。
0000111100000000 ->15
0000111000000000 ->14
0000110100000000 ->13
...
0000000000000000 ->0是否有一个简单的,明智的操作,将计算二进制?我目前的策略是将位提取为数字加一,做一些错误检查,然后用一个&0设置原始位,然后用一个\或掩码替换那个部分?
cellBinary = iterate(cellBinary, 16, 4);
function iterate(cellBinary, start, length)
{
let number = extractBits(cellBinary, start, length);
if(number < 15)
{
number++;
}
what = eraseIterator(what);
what = what|number;
return what;
}
function extractBits(what, start, length)
{
return ((1 << start ) -1) & (what >> (length - 1));
}
function eraseIterator(what)
{
what&16^1; //also iffy if this will work as intended.
what&32^1; //this is supposed to set 16-128 to 0.
what&64^1;
what&128^1;
return what;
}有什么更好的方法来实现这一点吗?注意:代码就是一个例子,我在寻找一个策略,而不是一个bug。
发布于 2020-03-26 06:58:12
您可以直接添加正确的数量: 256,100000000的二进制数。
例如,
000000000000 + 100000000 ->
000100000000
001000000000
001100000000
...
111100000000值为2^4-2^8的位
这就意味着这样的顺序:
00000000
00010000
00100000
00110000
...
11110000换句话说,加16。
https://stackoverflow.com/questions/60861845
复制相似问题