我想像这样连接两个数值的位:
+---------+---------+---------+-----------------+
| | val1 | val2 | concatenate val |
+---------+---------+---------+-----------------+
| decimal | 18 | 16 | 592 |
| hexa | 0x12 | 0x10 | 0x250 |
| binary | 0b10010 | 0b10000 | 0b1001010000 |
+---------+---------+---------+-----------------+
我尝试像这样与 + 连接:
const test = 0b10010, test1 = 0b10000
console.log(test+test1)//return 34
它不会连接值,而是将它们加在一起。
最佳答案
在添加之前,您可以将第一个值移动第二个值的按位长度。
const
add = (a, b) => (a << Math.ceil(Math.log2(b)) + 1) + b;
test = 0b10010,
test1 = 0b10000,
console.log(add(test, test1)); //
关于javascript - 如何在javascript中连接位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66398429/